合成属性和带下划线前缀的变量:这是什么意思? [英] Synthesized property and variable with underscore prefix: what does this mean?

查看:147
本文介绍了合成属性和带下划线前缀的变量:这是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

属性名称上的下划线前缀?


@synthesize window = _window; 我知道一般来说,它意味着一些类有一个窗口,但为什么使用 _window 而不是窗口?这是一个命名空间的东西吗?

解决方案



在其他语言中使用前面的 m m_ _ 来区分它们与本地声明的变量,并表示如果必要,它们应该有写入的访问器(无 classInstance .m_Variable = 5 )。



如果Objective-C程序员声明ivars遵循这个约定(并且它们应该)并使用基本语法 @synthesize _window; 然后属性的用法变得有点丑陋: classInstance._window = myWindow [classInstance set_window:myWindow] 。使用语法 @synthesize window = _window; 允许Obj-C程序员使用流行的编程标准(前面的ivars与 _ ),同时具有使用Apple标准的属性访问器 classInstance.window = myWindow [classInstance setWindow:myWindow]


Possible Duplicate:
Underscore prefix on property name?

What does this mean? @synthesize window=_window; I know that in general it means that 'some class' has a window, but why use _window instead of just window? Is this a namespace thing?

解决方案

I'll give a go at describing this programming convention in basic English.

It is a very common convention in other languages to name member variables with a preceding m, m_, or _ to distinguish them from locally declared variables and to signify that they should have accessors written, if necessary (no classInstance.m_Variable = 5).

If an Objective-C programmer declares ivars following this convention (and they should) and uses the basic syntax @synthesize _window; then the usage for the property becomes somewhat ugly: classInstance._window = myWindow or [classInstance set_window:myWindow]. Using the syntax @synthesize window=_window; allows the Obj-C programmer to utilize a popular programming standard (preceding ivars with _) while simultaneously having property accessors that use the Apple standard classInstance.window = myWindow and [classInstance setWindow:myWindow].

这篇关于合成属性和带下划线前缀的变量:这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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