[英]Is @synthesize required for readwrite property?
從Xcode 4.4開始具有默認的綜合屬性。 它會自動生成:
@synthesize name = _name;
readwrite vs readonly決定綜合屬性是否具有綜合訪問器(readwrite具有setter,並且是默認值,而readonly沒有)。
因此,我得出的結論是@synthesize name = _name;
對於讀寫不是必需的,但對於只讀是必需的
但是,在Apple的spritekit冒險代碼( 冒險代碼下載鏈接 )中, APAAdventureScene.m:
在示例中將合成“ heroes”(讀寫)。 如果不合成,則會出現以下錯誤:使用未聲明的標識符“ _heroes”
讀寫屬性需要@synthesize
嗎,我感到困惑?
謝謝
@interface APAAdventureScene () <SKPhysicsContactDelegate>
...
@property (nonatomic, readwrite) NSMutableArray *heroes; // our fearless adventurers
@property (nonatomic) NSMutableArray *goblinCaves; // whence cometh goblins
...
@end
@implementation APAAdventureScene
@synthesize heroes = _heroes;
- (id)initWithSize:(CGSize)size {
...
_heroes = [[NSMutableArray alloc] init];
_goblinCaves = [[NSMutableArray alloc] init];
...
}
- (void)updateWithTimeSinceLastUpdate:(CFTimeInterval)timeSinceLast {
// Update all players' heroes.
for (APAHeroCharacter *hero in self.heroes) {
[hero updateWithTimeSinceLastUpdate:timeSinceLast];
}
// Update the caves (and in turn, their goblins).
for (APACave *cave in self.goblinCaves) {
[cave updateWithTimeSinceLastUpdate:timeSinceLast];
}
}
@end
只要您使用的是現代LLVM編譯器(現在默認為1年以上),就不再需要@synthesize
。
readwrite
是默認設置,因此兩個屬性均為讀/寫。 在發布的代碼中沒有理由使用@synthesize
行。
唯一的例外是是否為readwrite
屬性同時提供“ setter”和“ getter”。 這樣就不會自動生成ivar。 對於readonly
屬性,如果提供顯式的“ getter”,則不會生成ivar。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.