在ios应用程序中使用@synthesize
我在iTunesU的iOS开发课程中观看了美妙的Paul Haggerty(导致谁不需要刷新基础知识?),他说了一些我并不知道的东西:
“我们永远不会访问underbar(_符号)variables”
然后,他继续谈论如何使用@synthesize variable = _variable
声明variables时,@ @synthesize variable = _variable
是编译器在后台生成的代码,以及setter和getter。 从本质上说,代码永远不会出现在你的应用程序。
在我迄今为止写的所有iOS应用程序中,我总是在头文件中使用@synthesize VARIABLE_NAME = _VARIABLE_NAME;
声明我的variables,并在@synthesize VARIABLE_NAME = _VARIABLE_NAME;
自从看演讲之后,我现在感到困惑,因为我的代码中是否应该使用@synthesize
。
我应该只使用财产声明? 如果我在代码中使用合成声明,它有什么不同?
既然哈格蒂先生不使用它,那为什么呢? (考虑到他是一个iOS的半神)。 我非常想做我一直在做的事情。
任何人都在意澄清这个问题?
Xcode 4.0 Developer Preview 4发行说明。 添加属性的默认自动合成(iOS和64位OS X)。 您不需要编译器的实现部分中的@synthesize
指令来合成声明属性的访问器。
所以
@synthesize ivar = _ivar;
如果你忽略它,完全一样。
不再需要合成。 今年的WWDC会议也包括了这一点。 只需使用属性。
不包括“@synthesize VARIABLE_NAME = _VARIABLE_NAME”将完全相同,因为如果您不添加任何内容,编译器会自动添加。
与Xcode 4.5或以上。 IDE为您编写@synthesize
语句。
@synthesize
语句只为你写setter
和getter
。
那, _variable_name
是即时variables。
variable_name
只是一个默认返回_variable_name
的值的方法。
当使用variable = <Statement or value>. its calling the
variable = <Statement or value>. its calling the
setVarable_Name method to set the
默认method to set the
_variable_name`。
希望它有帮助。