Objective-C:_variable [英] Objective-C: _variable
问题描述
好的,这个问题以前一定是问过的,但我看起来很生气,没有发现任何东西:
我的iphone应用程序中有一个简单的数组,我定义如下:
@property(nonatomic,strong)NSArray * pages;
@synthesize pages = _pages;
我在Apples示例代码中看到了这个,认为这是编写自我的好捷径.pages(即_pages替换self.pages)就像这样:
_pages = [[NSArray alloc] init];
但是Apple又有这个功能(并不完全像这样,但是看起来好像它们会随机交换):
self.pages = [NSKeyedUnarchiver unarchiveObjectWithData:contents];
最后:
[_ pages release];
这完全让我感到困惑。 _pages和self.pages之间有什么区别?
感谢您的帮助。
_pages
是对象的 ivar 的名称。 页面
是不同的属性名称。
因此, @synthesize pages = _pages;
最后告诉页面
是ivar _pages
的属性。
您将通过 _pages
在初始化程序和dealloc方法中嵌入ivar直接访问。其他地方,属性名称用于获取/设置其值。
OK, this must have been asked before but I looked like mad and found nothing:
I have a simple array in my iphone app which I define like so:
@property (nonatomic, strong) NSArray *pages;
@synthesize pages = _pages;
I've seen this in Apples sample code and thought this is a nice short-cut for writing self.pages (i.e. _pages replaces self.pages) like so:
_pages = [[NSArray alloc] init];
but then Apple has this again (not exactly like this, but it appears as if they keep swapping randomly):
self.pages = [NSKeyedUnarchiver unarchiveObjectWithData:contents];
And finally:
[_pages release];
Which totally confuses me. What would be the difference between _pages and self.pages?
Thanks for your help.
_pages
is the name of the object's ivar. pages
is the property name which is different.
So, @synthesize pages = _pages;
finally tells that pages
is the property for the ivar _pages
.
You will encouter the ivar direct access via _pages
in initializers and dealloc methods. Every where else, the property name is used to get/set its value.
这篇关于Objective-C:_variable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!