在 Obj-C 中使用访问器——这只是语义吗?
可能的重复:
我应该使用属性还是直接引用内部访问实例变量?
我一直想知道:是否有任何特殊原因总是使用或不使用访问器方法(方括号或点符号)?每当我合成属性时,我都会这样做...
@synthesize aProperty = _aProperty;
然后在我的代码中,我总是使用 self.aProperty 而不是 _aProperty。
在我看来,始终使用访问器似乎是合乎逻辑的。然而,在大多数示例/教程代码中,作者似乎与这种用法有些不一致,有时 self.aProperty
,有时 _aProperty
,有时甚至只是 aProperty< /code> (没有在合成中设置别名)。谁能解释为什么建议或不建议始终使用访问器,或者在什么情况下不建议使用?
Possible Duplicate:
Should I use properties or direct reference when accessing instance variables internally?
I have always wondered: is there any particular reason always to use or not use accessor methods (either square brackets or dot notation)? Whenever I synthesize properties, I do so like this...
@synthesize aProperty = _aProperty;
And then throughout my code I always use self.aProperty
instead of _aProperty
.
It seems to me logical to always use the accessors. However, in most sample/tutorial code, authors seem to be somewhat inconsistent with this usage, sometimes self.aProperty
, sometimes _aProperty
, and sometimes even just aProperty
(without having set the alias in synthesize). Can anyone explain why it would or would not be advisable to always use the accessors, or in which cases would it be not advisable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如我一直听到的那样,底线是:您应该始终使用合成的 getter 和 setter。但是,有时您需要直接访问变量,特别是当您可能覆盖合成的 getter 或 setter 以返回/设置一些处理后的数据,但还需要从类内部访问原始数据时。
示例:
假设在此示例中您想使用默认设置器,但您想覆盖 getter 以始终返回名字的规范化版本(抱歉,我知道这很奇怪,但我可以在我的顶部想到一个更好的示例)头)。所以你可以做这样的事情。
每当您使用 [self firstName] 或 self.firstName 时,您都将通过 getter 访问该变量并返回小写版本。但是,如果您仍然需要访问原始变量,您可以使用 _firstName 直接访问它。
Bottom line as I've always heard it: you should always use the synthesized getter and setter. However, there are times when you need to access the variable directly, especially when you may override the synthesized getter or setter to return/set some processed data but need to also access the raw data from inside the class.
Example:
Say in this example you wanted to use the default setter but you wanted to override the getter to always return a normalized version of the first name (sorry, I know its weird but I can think of a better example off the top of my head). So you can do something like this.
Anytime that you use [self firstName] or self.firstName you will access the variable through the getter and get back a lowercase version. However, if you needed to still access the raw variable you could use _firstName to directly access it.
据我所知,在任何情况下都不建议使用访问器。这一切都只是语义;使用访问器意味着您可以根据需要重用这些变量名称(aProperty),这在自定义设置器中非常有用。然而,这也意味着您要牢记范围并使用 self.aProperty。
当我教新的初级开发人员时,我让他们使用访问器,因为这迫使他们牢记范围。
但我对自己没有理会它们而感到内疚,只是合成变量并直接访问它(不使用 self )。
-- 由于评论而更新 --
我把这个问题当作一个更高级别的问题,但是从程序上来说绝对值得注意的是,下面的评论绝对是 100% 正确的,我为没有澄清这一点而深表歉意。
访问器不应该在构造函数和解构造函数中使用,因为这些子类可能不稳定,这可能会导致崩溃。
To my knowledge there is no situation in which its not advisable to use accessors. Its all just semantics; using accessors means you can reuse those variable names (aProperty) if you like, which can be nice in custom setters. However, it also means that you keep scope in mind and use self.aProperty.
When I teach a new junior developer, I have them use accessors as it forces them to keep scope in mind.
But I'm more than guilty myself of not bothering with them, just synthesizing the variable and accessing it directly (without using self).
-- Update due to comments --
I took the question as a higher-level question, however it is definitely worth noting on a programmatic basis that the comments below are absolutely, 100% correct, and I do apologize for not clarifying this.
Accessors should never be used in constructors and de-constructors, as these subclasses may not be stable and this can cause crashes.