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

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

问题描述

我相信我大部分都了解属性.我的问题是,如果我有一个实例变量的属性,并且我正在我的实现文件中的一个方法中设置或检索它,我应该使用 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.

使用 setter 很重要,因为它为其他代码提供了一个观察更改的操作(键值观察),以及子类有机会覆盖 setter 并进行任何其他所需的调整.

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

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

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