0

从 Xcode 4.4 开始具有默认综合属性。它会自动生成:

  @synthesize name = _name;

资源

source2

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
4

1 回答 1

2

@synthesize只要您使用现代 LLVM 编译器(现在已经超过 1 年的默认值),就不再需要任何东西了。

readwrite是默认值,因此这两个属性都是读/写的。@synthesize发布代码中的行没有理由。

唯一的例外是,如果您明确地为readwrite属性提供“setter”和“getter”。然后 ivar 不会自动生成。对于readonly属性,如果您提供明确的“getter”,则不会生成 ivar。

于 2013-10-03T05:37:07.787 回答