在其中一项任务中,我必须重写超类的游戏逻辑的getter方法(所以该方法将获得游戏逻辑的子类而不是原来的方法)。如何知道是否未声明@synthesize会导致“使用未声明的标识符”?
CardGameViewController.h:
#import <UIKit/UIKit.h>
#import "Deck.h"
#import "CardGame.h"
@interface CardGameViewController : UIViewController
@property (nonatomic) NSUInteger startingCardCount; // abstract
@property (strong, nonatomic) CardGame *game;
- (Deck *)createDeck; // abstract
- (void)updateCell:(UICollectionViewCell *)cell usingCard:(Card *)Card; // abstract
@end
CardGameViewController.m:
#import "CardGameViewController.h"
...
// no @synthesize here, but works fine.
- (CardGame *)game
{
if (!_game) _game = [[CardGame alloc] initWithCardCount:self.startingCardCount
usingDeck:[self createDeck]];
return _game;
}
...
@end
SetCardGameViewController.m:
...
@interface TSSetCardGameViewController()
@property (strong, nonatomic) CardGame *game;
@end
@implementation TSSetCardGameViewController
@synthesize game = _game; // Compiler *will* complain if this line is commented out.
- (CardGame *)game
{
if (!_game) _game = [[SetCardGame alloc] initWithCardCount:self.startingCardCount
usingDeck:[self createDeck]];
return _game;
}
...
@end
然后我得到了 “使用未声明的标识符” 为 “_game” 。所以我宣布
@property (strong, nonatomic) CardGame *game;
但我得到了同样的错误,所以我用“self.game”来代替,这就造成了不好的访问异常。 我无法在谷歌找到任何东西,所以我修修补补周围,直到我发现,这解决了这个问题:
@synthesize game = _game;
现在,我的问题是为什么。我的理解是Xcode的新版本为我做了合成,除非我重写它的getter和setter。我确实重写了getter,但不是setter,所以Xcode在技术上应该自动包含它。证明是,Xcode没有抱怨,直到我分类CardGameViewController,特别是取消了getter方法。 (FYI CardGameViewController还是其子类,既没有对*游戏setter方法)
所以我有点困惑。请帮忙!
这不是Xcode的,做自动合成,它的编译器。 – 2013-05-07 12:03:49
“SetCardGame”的属性是什么? – 2013-05-07 12:06:34
ott //我不确定你在问什么。它是CardGame的一个子类,有很多方法来计算纸牌游戏的分数。 – Skishnot 2013-05-07 12:13:29