【问题标题】:To support iOS 4.3, use "assign" instead of weak, but Interface Builder uses unsafe_unretained?要支持iOS 4.3,使用“assign”而不是weak,但是Interface Builder 使用unsafe_unretained?
【发布时间】:2012-09-08 10:02:27
【问题描述】:

要支持带有ARC的iOS 4.3,我认为正确的方法是使用assign

@property (assign, nonatomic) UIView *view;
@property (assign, nonatomic) MYNode *node;

正确吗?我还在Apple's doc for Transitioning to ARC 中看到以下内容:

对于声明的属性,你应该使用assign而不是weak;对于变量,您应该使用 __unsafe_unretained 而不是 __weak。

但是,如果我使用当前的 Xcode (4.4.1),将 Single View 应用程序目标更改为 4.3,然后按住 Ctrl 键将 UIButton 拖动到 .h 文件以创建插座,则生成的代码为:

@property (unsafe_unretained, nonatomic) IBOutlet UIButton *foo;

为什么会有区别,应该使用哪一个?

【问题讨论】:

    标签: objective-c automatic-ref-counting assign unsafe-unretained


    【解决方案1】:

    根据 llvm 文档中的4.1.1. Property declarations,“assign”和“unsafe_unretained”在属性声明中是等效的:

    • assign 意味着 __unsafe_unretained 所有权。
    • ...
    • unsafe_unretained 意味着 __unsafe_unretained 所有权。

    添加:在clang源代码http://clang.llvm.org/doxygen/SemaObjCProperty_8cpp_source.html你找到

    00523   // 'unsafe_unretained' is alias for 'assign'.
    00524   if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
    00525     PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
    

    【讨论】:

    • 但它们是否都暗示着相同的东西但仍然存在差异?
    • @JeremyL:我确信没有区别。我假设旧的 Xcode 版本不理解 @property (unsafe_unretained, ...)。我有一些旧版本,所以如果你真的有兴趣,我可以稍后测试。
    • 绝对没有区别。
    【解决方案2】:

    您引用了“哪些类不支持弱引用?”这个问题的答案的摘录。 — 实际上,我想,摘录仅适用于答案中列出的类。

    从我之前学习 ARC 时读到的内容来看,unsafe_unretainedassign 之间并没有真正的区别。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-07
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 2011-06-02
    相关资源
    最近更新 更多