在其中一項任務中,我必須重寫超類的遊戲邏輯的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