我应该在实施中使用自我关键字(属性)吗? [英] Should I Use self Keyword (Properties) In The Implementation?

查看:91
本文介绍了我应该在实施中使用自我关键字(属性)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我大部分时间都了解房地产.我的问题是,如果我有一个实例变量的属性,并且正在实现文件中的某个方法中设置或检索它,我应该使用self.myProperty还是仅使用myProperty?我知道其中任何一种都可以,但是我看到过多种约定,有时代码直接访问该变量,而其他时候则通过该属性访问该变量.

I believe I understand properties for the most part. My question is, if I have a property for an instance variable, and I am setting or retrieving it from within a method in my implementation file, should I use self.myProperty or just myProperty? I know either one works, but I have seen mixed conventions, sometimes code accesses the variable directly and other times through the property.

这样做有技术上的原因吗?只是惯例/个人喜好吗?而且我并不是指方法的参数名称与实例变量名称冲突的实例,这可能是使用该属性的原因之一(至少,在其他语言中,我对此一无所知一个).我假设当一个人在实现中使用该属性时,他们想利用声明属性的方式(即非原子的,保留),因此例如在一种方法中,一个人可以:

Is there a technical reason for doing this? Is it just convention/personal preference? And I'm not referring to the instances where the parameter name of a method collides with the instance variable name, which might be one reason to use the property (At least, in other languages, I don't know about this one). I assume that when one uses the property within the implementation, they want to take advantage of the way in which they declared the property (i.e. nonatomic, retain), so for example in a method, one can do:

self.myProperty = [someObject someAutoReleasedObject];

代替:

myProperty = [[someObject someAutoReleasedObject] retain];

这是原因吗?因此,仅在某些情况下使用该属性会比较好吗?

Is this the reason? So are there only certain situations in which it would be good to use the property?

我是Objective-C的新手,这是我感到困惑的几件事之一.到目前为止,我只是在最有可能的错误假设下直接访问了实例变量,即通过该属性实际上会调用/发送方法/消息并增加不必要的开销.我敢肯定我错了,但是即使开销的差异可以忽略不计(即使有的话),当人们可以直接访问该变量时,为什么还要选择添加它呢?

I'm new to Objective-C, and this is one of the few things that has me confused. So far I've just accessed the instance variable directly, under the most likely false assumption that going through the property actually calls/sends a method/message and adds unnecessary overhead. I'm pretty sure I'm wrong with this, but even if the difference in overhead is negligible (If there even is any), why would one choose to add it in when one can simply directly access the variable?

我很确定自己的想法是错误的,这就是为什么我在这里问.

I'm pretty sure I'm wrong in my thinking, which is why I'm asking here.

推荐答案

首先,您不应根据Apple文档(出于充分的理由)在init或dealloc中使用setter或getter.

First, you should not use setters or getters in init or dealloc according to Apple documentation (and for good reasons).

除此之外,如果有变量,通常应该使用setter来设置变量.

Other than that, you should generally use the setter for setting the variable if there is one.

通常,我不会在实现中使用getter来访问ivar,但是有时候有必要这样做.特别是,如果您希望getter可以进行一些计算或检查,或者您希望允许子类覆盖其行为.

Usually I do not bother using the getter for accessing the ivar from within the implementation, but there are times when it is necessary. In particular, if you expect the getter might do some caclulation or checking, or if you want to allow for subclasses to override the behaviour.

当然,在实现中使用getter更通用,更安全,但通常也没有意义和浪费.做出选择.

Certainly, using the getter in the implementation is more general and safer, but it is also typically pointless and wasteful. Make your choice.

使用设置器非常重要,因为它为其他代码提供了观察更改的关键(观察关键值),并且子类有机会覆盖设置器并进行任何其他需要的调整.

Using the setter is important as it gives an opertunity for other code to observe the changes (Key Value Observing), as well as subclasses a chance to override the setter and make any other adjustments required.

但是,我强烈建议您为ivar和财产使用一个不同的名称.常规约定是下划线前缀(_),尽管我本人使用i_作为前缀,以避免与Apple的私人用法混淆.这样一来,您就不会意外使用错误的密码:

However one thing I highly recommend is to use a different name for your ivar and your property. The normal convention is an underscore prefix (_), although I personally use i_ as the prefix to avoid any confusion with Apple's private usage. That way you cannot accidently use the wrong one:

self.name // use property
i_name // use ivar
self.i_name // syntax error
name // syntax error

这篇关于我应该在实施中使用自我关键字(属性)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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