简体   繁体   English

读写属性是否需要@synthesize?

[英]Is @synthesize required for readwrite property?

From Xcode 4.4 onwards has Default Synthesis Of Properties. 从Xcode 4.4开始具有默认的综合属性。 It generates this automatically: 它会自动生成:

  @synthesize name = _name;

source 资源

And from source2 source2

readwrite vs readonly determines whether a synthesized property has a synthesized accessor or not (readwrite has a setter and is the default, readonly does not). readwrite vs readonly决定综合属性是否具有综合访问器(readwrite具有setter,并且是默认值,而readonly没有)。

Therefore, I've concluded that @synthesize name = _name; 因此,我得出的结论是@synthesize name = _name; is not required for readwrite but it's needed for readonly 对于读写不是必需的,但对于只读是必需的

However, in Apple's spritekit Adventure code ( adventure code download link ), APAAdventureScene.m: 但是,在Apple的spritekit冒险代码( 冒险代码下载链接 )中, APAAdventureScene.m:

"heroes" (readwrite) is synthesize in the example. 在示例中将合成“ heroes”(读写)。 If it's not synthesize it will give this error: Use of undeclared identifier '_heroes' 如果不合成,则会出现以下错误:使用未声明的标识符“ _heroes”

Is @synthesize required for readwrite property, I'm confuse? 读写属性需要@synthesize吗,我感到困惑?

Thank you 谢谢

 @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

@synthesize isn't required for anything anymore as long as you are using a modern LLVM compiler (the default for over 1 year now). 只要您使用的是现代LLVM编译器(现在默认为1年以上),就不再需要@synthesize

readwrite is the default so both properties are read/write. readwrite是默认设置,因此两个属性均为读/写。 There is NO reason for the @synthesize line in the posted code. 在发布的代码中没有理由使用@synthesize行。

The only exception to this is if you explicitly provide both the "setter" and "getter" for a readwrite property. 唯一的例外是是否为readwrite属性同时提供“ setter”和“ getter”。 Then the ivar is not automatically generated. 这样就不会自动生成ivar。 For a readonly property the ivar isn't generated if you supply an explicit "getter". 对于readonly属性,如果提供显式的“ getter”,则不会生成ivar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM