从 Xcode 4.4 开始具有默认综合属性。它会自动生成:
@synthesize name = _name;
readwrite 与 readonly 确定合成属性是否具有合成访问器(readwrite 有一个 setter 并且是默认值,readonly 没有)。
因此,我得出的结论@synthesize name = _name;
是 readwrite 不需要,但 readonly 需要
但是,在苹果的 spritekit Adventure 代码(冒险代码下载链接)中, APAAdventureScene.m:
“英雄”(读写)在示例中被合成。如果它没有合成它会给出这个错误:Use of undeclared identifier '_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