问题描述
我在iOSU开发的iTunesU课程中观看精彩的Paul Haggerty(因为谁不需要刷新基础知识?)他说了一些我不知道的事情:
I was watching the wonderful Paul Haggerty in the iTunesU courses for iOS development (cause who doesn't need to refresh on the basics?) and he said something that I wasn't aware of:
然后他继续讨论如何使用@property声明变量, @synthesize variable = _variable
是编译器在幕后生成的代码,以及作为二传手和吸气鬼。基本上,代码永远不会出现在您的应用程序中。
He then went on to talk about how when you use @property to declare your variables,@synthesize variable = _variable
is code that's generated behind the scenes by the complier, as well as the setter and getter. Essentially that code never should appear in your app.
在我迄今为止编写的所有iOS应用程序中,我总是使用 @property
声明我的变量我的头文件和 @synthesize VARIABLE_NAME = _VARIABLE_NAME;
自从观看讲座以来,我现在很困惑我是否应该使用 @synthesize
在我的代码中。
In all of my iOS apps I've written thus far, I always declare my variables using @property
in my header file and @synthesize VARIABLE_NAME = _VARIABLE_NAME;
Since watching the lecture, I'm now confused as to if I should be using @synthesize
in my code at all.
我应该只使用属性声明吗?如果我在代码中使用合成声明,它会有什么不同呢?
Should I just use the property declaration? What difference does it make, if any, if I use the synthesize declaration in my code?
由于Haggerty先生不使用它,那我为什么呢? (考虑到他是一个iOS半神)。我非常觉得做我一直在做的事情是不好的。
Since Mr. Haggerty doesn't use it, then why do I? (considering he's sort of an iOS demi-god). I very much feel like it's bad form to do what I've been doing.
有人想澄清这个问题吗?
Anyone care to clarify that issue?
推荐答案
添加默认的属性自动综合(iOS和64位OS X)。在实现部分中,您不需要 @synthesize
指令,以便编译器合成声明属性的访问器。
Xcode 4.0 Developer Preview 4 Release Notes. Adds default automatic synthesis of properties (iOS and 64-bit OS X). You don’t need the @synthesize
directive in the implementation sections for the compiler to synthesize accessors for declared properties.
所以
@synthesize ivar = _ivar;
如果省略则完全相同。
这篇关于在ios应用程序中使用@synthesize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!