如果 name 是 NSString,那么 _name 和 self.name 有什么区别
有什么区别
NSString *theNameToDisplay = _name;
如果我打电话或者
NSString *theNameToDisplay = self.name;
我知道这可能是一个愚蠢的问题,但我看到两个版本都被大量使用,并且我没有发现输出有什么区别,那么
?谢谢!
What is the defference if I called
NSString *theNameToDisplay = _name;
or
NSString *theNameToDisplay = self.name;
I know it might be a silly question but I see both versions used a lot and I don't spot a difference in the output?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
假设您的 .m 文件中有此行(并且没有任何重写方法来直接访问 _name),
这意味着当您尝试以下操作时,属性
name
(self.name) 将使用变量 _name访问它。在这种情况下,self.name
等于 _name但如果您有 name 的动态属性,如下所示:
那么就有区别了。
self.name
将始终返回 1234,但 _name 不能等于该值。示例:
结果:
Assume you have in your .m file this line (and don't have any overriden methods to direct access to _name)
It mean that property
name
(self.name) will use variable _name when you try to access it. In this caseself.name
is equal to _nameBut if you have dynamic property for name, something like this :
then there is a difference.
self.name
will always return 1234, but _name can be not equal to this value.Example:
Result:
在问这样的问题之前,你最好阅读任何 Objective C 属性教程...试试这个 http://www.raywenderlich.com/2712/using-properties-in-objective-c-tutorial 或任何其他。
如果您创建了一个属性,您必须(好吧,应该)通过它访问 ivar,以便调用 setter 方法:
Before asking such question you had better read any objective c properties tutorial...try this http://www.raywenderlich.com/2712/using-properties-in-objective-c-tutorial or any other.
If you created a property you must(ok, should) access an ivar through it so that setter method is called:
第一个是直接访问变量。
第二个是通过属性访问它。
The first one is to access a variable directly.
The second one is accessing it through the property.
考虑下面的代码:
这可能会导致崩溃,因为当您设置属性时,旧值将被释放,因此
theNameToDisplay
成为一个悬空指针。请注意,对于NSString
这不一定会发生,尤其是对于文字字符串,但它可能会发生。当您启用 ARC 时,也不会发生这种情况。另一方面,
self.name
大致相当于:[[_name keep] autorelease]
,因此theNameToDisplay
仍然保持有效,直到该方法返回。Consider the following code:
This may result in a crash because when you set the property, the old value will be released, so
theNameToDisplay
becomes a dangling pointer. Note that withNSString
this does not necessarily happen, especially with literal strings, but it could. It also won't happen when you have ARC enabled.self.name
on the other hand is roughly equivalent to:[[_name retain] autorelease]
sotheNameToDisplay
would still remain valid until the method returns.请参阅有关属性的setter部分的其他答案,这是关于getters的:
通常,使用getter(
self.name
)或直接使用ivar(_name
)没有区别>、name
、name_
根据您的喜好)。然而,在某些情况下,可能会发生一些(半)智能的事情,并且 getter 隐藏了一些魔法。想象一下这个自定义 getter:
或者想象一下,您重新设计了您的类,并且
_name
ivar 被删除,取而代之的是包含更多信息的person
属性,并且您希望您的代码仍然可以工作没有太多麻烦:人们可能喜欢也可能不喜欢这种方法,但这超出了这个问题的范围。关键是,这可能会发生。所以更好的选择是始终使用吸气剂。
See the other answers about the setter part of properties, this is about getters:
Normally, there is no difference in using the getter (
self.name
) or the ivar directly (_name
,name
,name_
depending on your taste).However, there might be cases where something (semi-)intelligent is happening under the hood, and the getter hides some magic. Imagine this custom getter:
Or imagine that you rework your class and the
_name
ivar gets dropped in favor of aperson
property encompassing more information, and you want your code to still work without much hassle:People may or may not like such an approach, but that is outside the scope of this question. The point is, it may happen. So the better choice is to always use the getter.
在分配给其他对象时实际上没有区别,但如果以其他方式完成分配,例如
self.name = theNameToDisplay;
,则会产生很大的差异,这将与theNameToDisplay
将受到@property
属性的影响。really no difference, in assigning to other object but it will make great deal of difference if the assignment is done other way around means like
self.name = theNameToDisplay;
this will be different as the value intheNameToDisplay
will get affected by the@property
attributes.