@属性 用ARC定义:strong是默认值,但是基本类型的默认值是多少?

@property definitions with ARC: strong is the default value, but what's the default value of basic types?

我们知道,在ARC规则下,默认值是strong,所以使用

就可以了
@property NSString *downloadPath

但是为什么

@property (readonly) CGFloat progress
@property (readonly) BOOL isSSL 

好吗?由于它们是基本类型,因此应使用 assign。我可以这样理解ARC规则吗:

当属性为基本类型时,默认值为assign,当属性为对象类型时,默认值为strong

你是对的!另一个默认值 属性 是 atomic

来自 clang documentation 关于所有权的推断:

If an object is declared with retainable object owner type, but without an explicit ownership qualifier, its type is implicitly adjusted to have __strong qualification.

原因可以在 same page:

上找到

Using strong by default is safe and consistent with the generic ARC rule about inferring ownership. It is, unfortunately, inconsistent with the non-ARC rule which states that such properties are implicitly assign. However, that rule is clearly untenable in ARC, since it leads to default-unsafe code.

如文档所述,默认的所有权限定符是 assign,但是对于可保留类型(又名对象),默认限定符更改为 strong,这有助于开发人员编写安全的代码。

默认值是多少?

基本类型 - 基本类型和 struct 类型 - 是堆栈分配的,它们的值在赋值时被复制。对于这样的类型 strongweakunsafe_unretainedcopy 都是没有意义的——没有堆对象被引用并且值总是被复制。

因此其中 none 将成为默认值。

剩下 assign(并且文档一致)。所以相信你的直觉!

HTH