关于 ARC 中只读 @property 的问题
在我的接口 (.h) 文件中,
@property(readonly) NSString *foo;
在我的实现 (.m) 文件中,我
@synthesize foo;
打开了 ARC,编译器给出了以下错误:自动引用计数问题:ARC 禁止合成具有未指定所有权或存储属性的 Objective-C 对象。
如果我添加 strong
、weak
或 copy
,错误就会消失到该物业。这是为什么呢?为什么对于只读属性来说,这些东西之间会有任何差异,这些差异是什么,以及为什么程序员必须担心它们?为什么编译器不能智能地推断出只读属性的默认设置?
我遇到的另一个问题是:strong
、weak
或 copy
是 ARC 中唯一有意义的东西,对吗?我不应该再使用 retain
和 assign
,不是吗?
In my interface (.h) file, I have
@property(readonly) NSString *foo;
and in my implementation (.m) file, I have
@synthesize foo;
With ARC turned on, the compiler gives me this error: Automatic Reference Counting Issue: ARC forbids synthesizing a property of an Objective-C object with unspecified ownership or storage attribute.
The error goes away if I add a strong
, weak
, or copy
to the property. Why is this? Why would there be any differences between these things for a read-only property, what are those differences, and why does the programmer have to worry about them? Why can’t the compiler intelligently deduce a default setting for a read-only property?
Another question while I’m at it: strong
, weak
, or copy
are the only things that make sense in ARC, right? I shouldn’t be using retain
and assign
anymore, should I?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您声明了一个没有支持 ivar 的
@property
。因此,当编译器看到@synthesize时,它会尝试为您合成一个支持ivar。但你还没有指定你想要什么样的伊瓦尔。应该是__strong
吗?__weak
?__unsafe_unretained
?最初,属性的默认存储属性是assign
,它与__unsafe_unretained
相同。但在 ARC 下,这几乎总是是错误的选择。因此,他们要求您指定您想要哪种 ivar,而不是合成不安全的 ivar。You've declared a
@property
that doesn't have a backing ivar. Thus, when the compiler sees@synthesize
, it tries to synthesize a backing ivar for you. But you haven't specified what kind of ivar you want. Should it be__strong
?__weak
?__unsafe_unretained
? Originally, the default storage attribute for properties wasassign
, which is the same as__unsafe_unretained
. Under ARC, though, that's almost always the wrong choice. So rather than synthesizing an unsafe ivar, they require you to specify what kind of ivar you want.使用最新版本的 Xcode 和最新的 clang 编译器,此错误不再发生。您可以在接口中将属性指定为
@property(nonatomic, readonly) NSObject *myProperty;
,在实现中综合它,并且生成的 ivar 假定为strong
。如果您想显式或选择弱,可以在原始属性中执行此操作,例如@property(nonatomic,readonly,retain)。 Objective-C 正在逐渐减少冗余。With the latest version of Xcode and recent clang compilers, this error no longer occurs. You can specify a property as
@property(nonatomic, readonly) NSObject *myProperty;
in the interface, synthesize it in the implementation, and the resulting ivar is assumed to bestrong
. If you want to be explicit or choose weak, you can do so in the original property, like@property(nonatomic, readonly, retain)
. Objective-C is incrementally becoming less redundant.它在这里作为对其余代码的声明。
当您从代码的其他部分访问此类的属性时,您需要知道您获得的对象是强还是弱。
当 ARC 不存在时,它曾经更加明显,因为程序员需要这些信息。现在,ARC 使很多事情变得透明,所以这是真的,你可能想知道为什么它还在这里。
我认为设置一个约定非常容易,即没有关键字意味着
强
或意味着弱
。如果没有这样做,他们肯定是有原因的。It is here as a declaration to the rest of your code.
When you a access the property of this class from an other part of your code, you need to know if the object you get is strong or weak.
It used to be more obvious when ARC didn't exists, because the programmer needed this information. Now, ARC makes a lot of things transparent, so it is true, you might wonder why it is still here.
I assume it would be pretty easy to set the convention that no keyword means
strong
or meansweak
. If it hasn't been done, they surely had a reason.