Objective-C:@property 声明但没有实现 (@synthesize)

发布于 2024-12-05 16:24:07 字数 322 浏览 0 评论 0原文

据我了解, @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧人九事 2024-12-12 16:24:07

从 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.

暖阳 2024-12-12 16:24:07

.h 文件中的以下内容

@property (retain, nonatomic) NSString* userName;

语法糖

-(NSString*)userName;

-(void)setUserName:(NSString*)userName;

是.h 文件中的

,.m 中的 @synthesize 是通过尊重您的 retain 来保留其自己的实现的语法糖,< code>nonatomic 等,如您的属性声明中所指定。

即使您在 .h 文件中指定了合适的 getter、setter 签名,也无法在没有 @property 的情况下进行合成。

但是你可以离开@synthesize并实现你自己的getter和setter。

如果您同时使用了 @synthesize 和自定义 getter setter,则运行时会尊重您的实现并调用相同的实现。

having below in .h file

@property (retain, nonatomic) NSString* userName;

is syntactic sugar for

-(NSString*)userName;

-(void)setUserName:(NSString*)userName;

in the .h file

and @synthesize in .m is syntactic sugar for leaving for its own implementation by respecting your retain, 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.

像你 2024-12-12 16:24:07

当我声明 @property 但忘记实现它时会发生什么
使用@synthesize 还是手动?当我使用 @synthesize 时会发生什么
忘记@property声明?

你为什么不尝试一下呢?如果您忘记综合访问器或提供自己的访问器,编译器会警告您。 (如果您将其设置为将警告视为错误,这是一个好主意,则不可能错过警告。)第二种情况通常会导致编译错误。

如果我们总是需要实现 setter/getter 为什么我们需要
另外声明一下吗? @property 似乎在语义上是多余的
我和错误来源。

简而言之,@property 是一个属性声明,@synthesize 是一种可能的实现。一个属于标头(=公共接口描述),另一个属于实现文件。如果不这样的话你会怎么做?

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?

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.

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.

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?

围归者 2024-12-12 16:24:07

只是想补充一点,您也可以使用 @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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文