何时使用“self"访问属性 [英] When to access properties with 'self'

查看:26
本文介绍了何时使用“self"访问属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在本网站上阅读了许多有关此问题的问题,我了解以下内容:

I have read a number of questions on this site about this issue, I understand the following:

self.property 访问手动或@synthesize 创建的 getter/setter 方法.根据属性是否声明为保留、复制等,保留计数被正确修改,例如保留的属性,释放分配给带有retain"的新值的先前值,并将保留计数增加 1.

self.property accesses the getter/setter method created manually or by @synthesize. Depending upon whether the property is declared as retain, copy etc. the retain count is modified correctly, for example a retained property, releases the previous value assigned the new value with 'retain' and increments the retain count by 1.

属性通常用同名的实例变量声明(如果您手动进行赋值,则可能会有所不同).这一般是因为@synthesize 生成的访问器使用实例变量来引用内存中的对象,然后执行相关命令.

Properties are usually declared with instance variables of the same name (can be different if you make the assignment manually). This is generally because the @synthesize generated accessors use the instance variable to reference the object in memory and then perform the relevant commands.

我的问题是基于这样一个事实,即在许多示例中,self.property 和 property 可互换用于不同的事物,而我在确定规则时遇到了麻烦.Apple 文档中食谱"示例应用中的一个示例如下:

My question is based on the fact that in many examples, self.property and property are used interchangeably for different things and I'm having troubles determining the rules. One example in the 'Recipes' example app in the Apple Docs, has the following:

self.navigationItem.title = recipe.name;
nameTextField.text = recipe.name;    
overviewTextField.text = recipe.overview;    
prepTimeTextField.text = recipe.prepTime; 

还有……

self.ingredients = sortedIngredients;

这些属性中的每一个都有关联的同名私有实例变量.所有都以相同的方式使用非原子,保留"属性进行声明.每个都在dealloc中发布...

Each of these properties have associated private instance variables of the same name. All are declared in the same way with 'nonatomic, retain' attributes. Each are released in dealloc...

然而,'ingredients' 是通过 self 访问的,而 'prepTimeTextField' 是直接访问的.

Yet 'ingredients' is accessed through self and 'prepTimeTextField' is accessed directly.

访问方式不同的原因是什么?

What is the reason for the difference in access methods?

如果我正在访问一个视图的委托呢?或者一个核心数据对象作为保留属性被它的前一个视图控制器传递给一个视图控制器?

What about if I'm accessing a view's delegate? Or a core-data object which was passed to a view controller by it's previous view controller as a retained property?

非常感谢

推荐答案

您几乎总是想使用合成的 setter/getter 访问变量,即使您目前没有对它们做任何特别的事情.

you almost always want to access variables using the synthesized setters/getters, even if you aren't doing anything special with them at the moment.

如果随着您的应用程序的开发,您发现需要进一步验证/格式化变量,那么您可以只实现所需的 setter/getter 方法,如果您使用了合成方法,则会调用此代码.

if as your application develops, you find that you need to be doing further validation/formatting of variables, then you can just implement the required setter/getter method, and if you have used the synthesised methods this code will get called.

这通常是一个很好的习惯.

this is generally a good habit to get into.

这篇关于何时使用“self"访问属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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