如果 name 是 NSString,那么 _name 和 self.name 有什么区别

发布于 2025-01-03 14:09:15 字数 226 浏览 1 评论 0原文

有什么区别

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

空名 2025-01-10 14:09:15

假设您的 .m 文件中有此行(并且没有任何重写方法来直接访问 _name),

@synthesize name = _name;

这意味着当您尝试以下操作时,属性 name (self.name) 将使用变量 _name访问它。在这种情况下,self.name 等于 _name


但如果您有 name 的动态属性,如下所示:

-(NSString)name{
    return @"1234";
}

那么就有区别了。 self.name 将始终返回 1234,但 _name 不能等于该值。

示例:

_name = @"555";
NSLog(_name);
NSLog(self.name);

结果:

2012-02-09 14:27:49.931 ExampleApp[803:207] 555
2012-02-09 14:27:49.933 ExampleApp[803:207] 1234

Assume you have in your .m file this line (and don't have any overriden methods to direct access to _name)

@synthesize name = _name;

It mean that property name (self.name) will use variable _name when you try to access it. In this case self.name is equal to _name


But if you have dynamic property for name, something like this :

-(NSString)name{
    return @"1234";
}

then there is a difference. self.name will always return 1234, but _name can be not equal to this value.

Example:

_name = @"555";
NSLog(_name);
NSLog(self.name);

Result:

2012-02-09 14:27:49.931 ExampleApp[803:207] 555
2012-02-09 14:27:49.933 ExampleApp[803:207] 1234
旧情勿念 2025-01-10 14:09:15

在问这样的问题之前,你最好阅读任何 Objective C 属性教程...试试这个 http://www.raywenderlich.com/2712/using-properties-in-objective-c-tutorial 或任何其他。
如果您创建了一个属性,您必须(好吧,应该)通过它访问 ivar,以便调用 setter 方法:

- (void)setMyProp:(NSArray *)myProp {
   [myProp retain];
   [_myProp release];
   _myProp = myProp;
}

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:

- (void)setMyProp:(NSArray *)myProp {
   [myProp retain];
   [_myProp release];
   _myProp = myProp;
}
说谎友 2025-01-10 14:09:15

第一个是直接访问变量。
第二个是通过属性访问它。

The first one is to access a variable directly.
The second one is accessing it through the property.

寂寞清仓 2025-01-10 14:09:15

考虑下面的代码:

NSString *theNameToDisplay = _name;
self.name = @"foo";
NSLog(@"%@", theNameToDisplay);

这可能会导致崩溃,因为当您设置属性时,旧值将被释放,因此 theNameToDisplay 成为一个悬空指针。请注意,对于 NSString 这不一定会发生,尤其是对于文字字符串,但它可能会发生。当您启用 ARC 时,也不会发生这种情况。

另一方面,self.name 大致相当于:[[_name keep] autorelease],因此 theNameToDisplay 仍然保持有效,直到该方法返回。

Consider the following code:

NSString *theNameToDisplay = _name;
self.name = @"foo";
NSLog(@"%@", theNameToDisplay);

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 with NSString 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] so theNameToDisplay would still remain valid until the method returns.

兲鉂ぱ嘚淚 2025-01-10 14:09:15

请参阅有关属性的setter部分的其他答案,这是关于getters的:

通常,使用getter(self.name)或直接使用ivar(_name)没有区别>、namename_ 根据您的喜好)。

然而,在某些情况下,可能会发生一些(半)智能的事情,并且 getter 隐藏了一些魔法。想象一下这个自定义 getter:

-(NSString*)name
{
    if ( _name == nil ) return @"No name set";
    return _name;
}

或者想象一下,您重新设计了您的类,并且 _name ivar 被删除,取而代之的是包含更多信息的 person 属性,并且您希望您的代码仍然可以工作没有太多麻烦:

-(NSString*)name
{
    return self.person.name;
}

人们可能喜欢也可能不喜欢这种方法,但这超出了这个问题的范围。关键是,这可能会发生。所以更好的选择是始终使用吸气剂。

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:

-(NSString*)name
{
    if ( _name == nil ) return @"No name set";
    return _name;
}

Or imagine that you rework your class and the _name ivar gets dropped in favor of a person property encompassing more information, and you want your code to still work without much hassle:

-(NSString*)name
{
    return self.person.name;
}

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.

彩扇题诗 2025-01-10 14:09:15

在分配给其他对象时实际上没有区别,但如果以其他方式完成分配,例如 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 in theNameToDisplay will get affected by the @property attributes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文