为什么 Obj-C 属性默认所有权“分配"?而不是“强" [英] Why Obj-C property default ownership "assign" instead of "strong"

查看:56
本文介绍了为什么 Obj-C 属性默认所有权“分配"?而不是“强"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向旧项目添加 Swift 类.一切顺利,直到我尝试向 Swift 类添加一个属性.生成的标头无法编译.

I'm adding Swift classes to an old project. It went well, until I tried adding a property to the Swift class. The generated header doesn't compile.

我认为问题在于,在生成的代码中,Swift 省略了 strong 所有权,仅将其声明为 nonatomic.这通常应该足够了,因为@property 应该默认为 strong 所有权,对吗?

I think the problem is, in the generated code, Swift omitted strong ownership and only declared it as nonatomic. This should normally be enough, because @property should default to strong ownership, right?

所以基本上这些是等价的:

So basically these are equivalent:

  • @property (nonatomic) NSDate *aDate;
  • @property (nonatomic, strong) NSDate *aDate;

但是,就我而言,根据编译器消息,它似乎默认为 assign 而不是 strong.

But, in my case, it seems like it is defaulting to assign instead of strong, according to the compiler message.

我使用的是 Xcode 6 GM,并且该项目已打开 ARC.

I'm using Xcode 6 GM, and the project has ARC turned on.

知道为什么不默认为 strong 吗?我可以以某种方式改变它吗?

Any idea why it is not defaulting to strong? Can I change this somehow?

推荐答案

经过无数次实验,我发现了一个决定属性默认所有权行为的微妙之处:

After numerous experiments, I've found out a subtlety that determines the default ownership behavior of a property:

  • 如果头文件仅导入到启用 ARC 的类中,并且没有声明默认所有权,则此头文件中的属性的所有权为 strong.
  • 如果一个头文件被导入至少一个非ARC类,并且没有声明默认所有权,那么该属性的所有权是assign
  • If a header file is imported only into ARC-enabled classes, and there is no default ownership declared, then the ownership of the property within this header file is strong.
  • If a header file is imported into at least one non-ARC class, and there is no default ownership declared, then the ownership of the property is assign!

这也意味着,您不得将 -Swift.h 标头导入任何非 ARC 类,因为它会更改所有属性的行为并发出警告(在我的情况下是转换为错误).

This means also, you must not import a -Swift.h header into any non-ARC classes, as it will change the behavior of all the properties and emit warnings (which in my case were converted to errors).

很奇怪的恕我直言...

Pretty weird IMHO...

示例:

  • 我们有类SourceClassARCClass(启用ARC)和MRCClass(禁用ARC)
  • SourceClass.h 有:@property (nonatomic) NSDate *date;
  • we have classes SourceClass, ARCClass(ARC-enabled) and MRCClass(ARC-disabled)
  • SourceClass.h has: @property (nonatomic) NSDate *date;

微妙:

  • 如果我们只在ARCClass.hARCClass.m中添加#import "SourceClass.h"
    • 财产 date 拥有所有权 strong.
    • 声明等价于 @property (nonatomic, strong) NSDate *date;.
    • If we add #import "SourceClass.h" only in ARCClass.h or ARCClass.m,
      • the property date has ownership strong.
      • the declaration is equivalent to @property (nonatomic, strong) NSDate *date;.
      • 财产date 将拥有所有权assign 代替.
      • 声明被更改@property (nonatomic, assign) NSDate *date;.
      • the property date will have ownership assign instead.
      • the declaration is changed to @property (nonatomic, assign) NSDate *date;.

      这篇关于为什么 Obj-C 属性默认所有权“分配"?而不是“强"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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