属性名称的下划线前缀? [英] Underscore prefix on property name?

查看:118
本文介绍了属性名称的下划线前缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:


Possible Duplicate:
How does an underscore in front of a variable in a cocoa objective-c class work?

任何人都可以指示使用下划线的说明,我一直假设他们用于强调您正在访问iVar [_ window release]; ,而不是通过setter / getter方法访问iVar [[self window] release ]; [self.window release]; 我只想验证我的理解是否正确。

Can anyone point me to an explanation of the use of underscores, I have always assumed that they are used to highlight that you are accessing the iVar [_window release]; rather than accessing the iVar via a setter/getter method [[self window] release]; or [self.window release]; I just want to verify that my understanding is correct.

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UILabel *markerLabel;

@synthesize window = _window;
@synthesize markerLabel = _markerLabel;


推荐答案

对于ivar名称使用下划线是一个约定首先被苹果用于区分实际ivar和财产。

The use of an underscore for ivar names is a convention first used by Apple to differentiate between an actual ivar and a property. Many people have since adopted this convention.

这样做的原因是为了防止向ivar而不是给实际的setter分配一个新值的错误:

The reason this is done is to prevent the mistake of assigning a new value to an ivar instead of to the actual setter:

myIvar = newValue;

而不是

self.myIvar = myValue;

如果您不小心使用了顶层示例,则可能导致内存泄漏。下划线可防止您犯这个错误。

If you accidentally use the top example, you could cause a memory leak. The underscore prevents you from making that mistake.

这篇关于属性名称的下划线前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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