Objective-C是@synthesize必需还是可选的? [英] Objective-C is @synthesize required or optional?
问题描述
我已经测试了这个:
没有@synthesize,你可以使用
访问属性和私有字段 self.property_name = something;
_property_name =某事; //(用于getter和setter)
使用@synthesize property_name,可以访问属性和私有字段使用
self.property_name = something;
property_name = something;
我的问题是我们真的需要@synthesize吗?还是有一个我错过的更大的故事?
从Xcode 4.4开始,我们不需要这样做,它添加了一个名为默认合成属性的功能。
简单地说,它会自动生成:
@synthesize name = _name;
sorry I'm a newbie iOS developer, recently I've briefly heard that @synthesize is now @synthesize in a certain version of Xcode and that the compiler will auto synthesize and properties and assign _ to private variables.
I've tested this:
with out @synthesize, you can access properties and private field using
self.property_name = something;
_property_name = something; //(used in getter and setters)
with @synthesize property_name, you can access properties and private field using
self.property_name = something;
property_name = something;
My question is do we really need @synthesize anymore? or there is a bigger story I'm missing?
No we don't need to do that as of Xcode 4.4, which added a feature called Default Synthesis Of Properties.
Simply put, it generates this automatically:
@synthesize name = _name;
这篇关于Objective-C是@synthesize必需还是可选的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!