Objective-C:_variable [英] Objective-C: _variable

查看:110
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆