在带有ARC的Objective-C中,是否确实需要通常仅将非原子指定为属性属性? [英] In Objective-C with ARC, is it true that we usually only need to specify nonatomic as property attributes?

查看:91
本文介绍了在带有ARC的Objective-C中,是否确实需要通常仅将非原子指定为属性属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

奇怪的是,在Big Nerd Ranch iOS 5书(第73页)和《编程iOS 5书》(O'Reilly,第314页)(更新:甚至是Kochan的Objective-C书第四版),在ARC的上下文中,他们说properties属性的默认值为assign ...但是

It is strange that in Big Nerd Ranch iOS 5 book (p.73) and Programming iOS 5 book (O'Reilly, p.314) (updadte: even Kochan's Objective-C book Fourth edition), in the context of ARC, they say the default for properties attribute is assign... But Apple's documentation says the default is strong.

我还尝试了一个简单的程序,如果不指定strong,则该程序可以正常运行;如果指定了strong,则该程序也可以正常运行,而当使用assign时,编译器将显示警告,因此似乎默认确实是strong.

I also tried a simple program where if I don't specify strong, the program works ok, and if I specify strong, it works the same, and when assign is used instead, the compiler shows a warning, so it seems the default is indeed strong.

因此,在大多数情况下,我们想要

So if most of the time, we want

@property (nonatomic, readwrite, strong) NSMutableArray *foo;

那我们就可以写

@property (nonatomic) NSMutableArray *foo;

作为其他两个(readwritestrong)是默认设置吗?

as the other two (readwrite and strong) are the default?

推荐答案

读写能力强,实际上是ARC *下的默认设置.在手动引用计数下,assign是默认值.我更愿意明确指定这些参数,因为这样可以更清楚@property的参数是什么,而不是依靠阅读代码的人知道默认值是什么.

readwrite and strong, are indeed the default under ARC*. Under manual reference counting, assign was (is) the default. I prefer to explicitly specify these, because it makes it clearer what the @property's parameters are instead of relying on the person reading the code knowing what the defaults are.

* strong.否则,默认属性所有权类型与ivar声明中的所有权限定符匹配.因此,如果用__weak显式声明一个ivar,然后为它声明一个@property而没有所有权限定符,则综合属性将很弱.这些都记录在 Clang ARC文档中.

*strong is the default assuming you've either let the compiler synthesize an instance variable for you, or have declared an instance variable without an explicit ownership qualifier (in which case the ivar is __strong by default anyway). Otherwise, the default property ownership type matches the ownership qualifier in the ivar's declaration. So, if you explicitly declare an ivar with __weak, then declare an @property for it without an ownership qualifier, the synthesized property will be weak. This is all documented in the Clang ARC documentation.

这篇关于在带有ARC的Objective-C中,是否确实需要通常仅将非原子指定为属性属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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