问题描述
? @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]
。
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]
.
这篇关于合成属性和带下划线前缀的变量:这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!