從Xcode 4.4開始,具有默認的屬性綜合。它自動生成這樣的:是否需要readwrite屬性的@synthesize?
@synthesize name = _name;
而且從source2
讀寫VS只讀確定合成屬性是否具有合成訪問者或不(讀寫具有setter和是默認的,只讀確實不)。
因此,我的結論是,@synthesize name = _name;
不需要讀寫,但它需要只讀
然而,在蘋果的spritekit冒險碼(adventure code download link),APAAdventureScene.m:
「英雄」(讀寫)是合成在這個例子中。如果它不合成它會給出這個錯誤:使用未聲明的標識符'_heroes'
是@synthesize
需要爲readwrite屬性,我很困惑?
謝謝
@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
我可以知道投票的理由嗎? – user1872384