问题描述
可能的重复:
属性名称的下划线前缀?
这是什么意思?@synthesize window=_window;
我知道一般来说这意味着某个类"有一个窗口,但为什么使用 _window
而不是 window
>?这是命名空间的事情吗?
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.
在其他语言中,使用m
、m_
或_
来命名成员变量是一种非常常见的约定,以区别于它们本地声明的变量,并表示它们应该在必要时编写访问器(没有 classInstance.m_Variable = 5
).
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
).
如果一个 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]
.
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]
.
这篇关于带下划线前缀的综合属性和变量:这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!