关于 ARC 中只读 @property 的问题

发布于 2024-12-23 17:56:25 字数 567 浏览 3 评论 0原文

在我的接口 (.h) 文件中,

@property(readonly) NSString *foo;

在我的实现 (.m) 文件中,我

@synthesize foo;

打开了 ARC,编译器给出了以下错误:自动引用计数问题:ARC 禁止合成具有未指定所有权或存储属性的 Objective-C 对象。

如果我添加 strongweakcopy,错误就会消失到该物业。这是为什么呢?为什么对于只读属性来说,这些东西之间会有任何差异,这些差异是什么,以及为什么程序员必须担心它们?为什么编译器不能智能地推断出只读属性的默认设置?

我遇到的另一个问题是:strongweakcopy 是 ARC 中唯一有意义的东西,对吗?我不应该再使用 retainassign ,不是吗?

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 技术交流群。

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

发布评论

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

评论(3

时光是把杀猪刀 2024-12-30 17:56:25

您声明了一个没有支持 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 was assign, 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.

虐人心 2024-12-30 17:56:25

使用最新版本的 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 be strong. 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.

玻璃人 2024-12-30 17:56:25

它在这里作为对其余代码的声明。
当您从代码的其他部分访问此类的属性时,您需要知道您获得的对象是强还是弱。

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

Why can’t the compiler intelligently deduce a default setting for a read-only property?

I assume it would be pretty easy to set the convention that no keyword means strong or means weak. If it hasn't been done, they surely had a reason.

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