Objective-C:@property 声明但没有实现 (@synthesize)
据我了解, @property 在 .h 文件中声明 getter 和 setter,而 @synthesize 在 .m 文件中实现 getter 和 setter。此外,可以为 getter 和 setter 编写自己的实现,而不是使用 @synthesize。
那么如果我们总是需要实现 setter/getter 为什么我们需要额外声明它呢? @property 对我来说似乎在语义上是多余的,并且是错误的来源。
当我声明一个 @property 但忘记使用 @synthesize 或手动实现它时会发生什么?当我使用 @synthesize 并忘记 @property 声明时会发生什么?
As far as I understand @property declares the getter and setter in the .h file and @synthesize implements the getter and setter in the .m files. Furthermore it is possible to write your own implementation for the getter and setter instead of using @synthesize.
So if we always need to implement the setter/getter why do we need to additionaly declare it? @property seems to be semantical redundant to me and a source for bugs.
What happends when I declare a @property but forget to implement it with @synthesize or manually? What happends when I use @synthesize and forget the @property declaration?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 Xcode 4.4 开始,属性现在默认是合成的,所以是的,你是对的,这是多余的,Apple 删除了编写 @property 和 @synthesize 的需要,从现在开始只需使用 @property 即可。
Properties are now synthesized by default, since Xcode 4.4, so yes, you are right, it was redundant and Apple removed the need to write both @property and @synthesize, just use @property from now on.
.h 文件中的以下内容
语法糖
是.h 文件中的
,.m 中的
@synthesize
是通过尊重您的retain
来保留其自己的实现的语法糖,< code>nonatomic 等,如您的属性声明中所指定。即使您在 .h 文件中指定了合适的 getter、setter 签名,也无法在没有 @property 的情况下进行合成。
但是你可以离开@synthesize并实现你自己的getter和setter。
如果您同时使用了 @synthesize 和自定义 getter setter,则运行时会尊重您的实现并调用相同的实现。
having below in .h file
is syntactic sugar for
in the .h file
and
@synthesize
in .m is syntactic sugar for leaving for its own implementation by respecting yourretain
,nonatomic
, etc. as specified in your property declaration.you cannot synthesize with out @property, even if you specified suitable getter, setter signature in .h file.
But you can leave @synthesize and implement your own getter and setter.
If you did both @synthesize and also your custom getter setter, run-time respects your implementation and call the same.
你为什么不尝试一下呢?如果您忘记综合访问器或提供自己的访问器,编译器会警告您。 (如果您将其设置为将警告视为错误,这是一个好主意,则不可能错过警告。)第二种情况通常会导致编译错误。
简而言之,
@property
是一个属性声明,@synthesize
是一种可能的实现。一个属于标头(=公共接口描述),另一个属于实现文件。如果不这样的话你会怎么做?Why don’t you try that? If you forget to synthesize the accessors or provide your own, the compiler will warn you. (And if you have it set to treat warnings as errors, which is a good idea, it’s impossible to miss the warning.) The second case will usually lead to a compile error.
To put it simply,
@property
is a property declaration and@synthesize
is one possible implementation. One belongs to the header (= public interface description), the other to the implementation file. How would you do it if not this way?只是想补充一点,您也可以使用 @dynamic 而不是 @synthesize,尽管您很少需要这样做。 @dynamic 基本上是告诉编译器,您将以某种方式确保对 getter 和 setter 的调用将在运行时得到处理,无论是通过自己制作还是通过做一些额外的事情搞乱 Objective C 运行时的一些特殊功能。
Just wanted to add that you can also use @dynamic instead of @synthesize, although this is very rarely something you will need to do. @dynamic is basically telling the complier that you are somehow going to make sure that the calls to the getters and setters will be taken care of at run-time, either by making them yourself or by doing some extra messing around with some of the special features of the Objective C runtime.