关于ARC中readonly @property的问题 [英] Questions about a readonly @property in ARC

查看:183
本文介绍了关于ARC中readonly @property的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的介面(.h)档案中,我有

  @property(readonly)NSString * foo; 

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

  @synthesize foo; 

在ARC打开的情况下,编译器给出此错误:自动引用计数问题:ARC



如果我添加了一个,那么错误就会消失。 strong weak copy 为什么是这样?为什么对于只读属性,这些东西有什么区别,这些区别是什么,为什么程序员要担心它们?为什么编译器不能智能地推导出只读属性的默认设置?



我处于另一个问题: strong weak copy 是ARC唯一有意义的东西吗?我不应该再使用 retain assign 吗?

@property
。因此,当编译器看到 @synthesize 时,它会尝试为您合成备用ivar。但你没有指定你想要什么样的ivar。应该是 __ strong 吗? __ weak __ unsafe_unretained ?最初,属性的默认存储属性是 assign ,这与 __ unsafe_unretained 相同。然而,根据ARC,这几乎总是错误的选择。因此,不是合成不安全的ivar,他们需要你指定你想要什么样的ivar。


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?

解决方案

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.

这篇关于关于ARC中readonly @property的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆