Ios 读写属性是否需要@synthesis?
从Xcode 4.4开始,具有默认的属性合成。它会自动生成:Ios 读写属性是否需要@synthesis?,ios,objective-c,properties,synthesize,Ios,Objective C,Properties,Synthesize,从Xcode 4.4开始,具有默认的属性合成。它会自动生成: @synthesize name = _name; 来自 readwrite vs readonly确定合成属性是否有合成的访问器(readwrite有一个setter,是默认值,readonly没有) 因此,我得出结论,@synthesis name=\u name,但readonly需要它 然而,在苹果的spritekit冒险代码()中,APAAdventureScene.m: “英雄”(readwrite)在示例中是合成
@synthesize name = _name;
来自
readwrite vs readonly确定合成属性是否有合成的访问器(readwrite有一个setter,是默认值,readonly没有)
因此,我得出结论,@synthesis name=\u name,但readonly需要它
然而,在苹果的spritekit冒险代码()中,APAAdventureScene.m:
“英雄”(readwrite)在示例中是合成的。如果它不是合成的,则会出现以下错误:使用未声明的标识符“\u”
读写属性是否需要@synthesis
,我很困惑
多谢各位
@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
@界面场景()
...
@属性(非原子,读写)NSMutableArray*heroes;//我们无畏的冒险家
@属性(非原子)NSMutableArray*goblinCaves;//地精从哪里来
...
@结束
@虚拟场景的实现
@综合英雄=_英雄;
-(id)initWithSize:(CGSize)大小{
...
_英雄=[[NSMutableArray alloc]init];
_goblinCaves=[[NSMutableArray alloc]init];
...
}
-(void)updateWithTimeSinceLastUpdate:(CFTimeInterval)timeSinceLast{
//更新所有玩家的英雄。
为了(一个英雄角色*自我中的英雄。英雄){
[hero updateWithTimeSinceLastUpdate:timeSinceLast];
}
//更新洞穴(进而更新他们的地精)。
为了(阿帕卡夫*自我沉沦。地精洞穴){
[cave updateWithTimeSinceLastUpdate:timeSinceLast];
}
}
@结束
@synthetic
不再需要任何东西,只要您使用的是现代LLVM编译器(默认值现在超过1年)
readwrite
是默认值,因此这两个属性都是读/写属性。在发布的代码中没有理由出现@synthesis
行
唯一的例外是,如果为readwrite
属性显式提供“setter”和“getter”。那么ivar不会自动生成。对于readonly
属性,如果您提供一个明确的“getter”,则不会生成ivar。我同意这句话“没有理由在发布的代码中使用@synthesis行。”但是它给出了一个错误:使用未声明的标识符“\u heroes”不,它没有任何显式的setter或getter方法。我可以知道你投反对票的原因吗?