【问题标题】:Protocols & assign properties in iOS 5iOS 5 中的协议和分配属性
【发布时间】:2011-12-10 21:12:18
【问题描述】:

我正在尝试在 iOS 5 中创建自己的自定义委托。 在 iOS 4 中,我通常使用 'Assign' 属性

@property(nonatomic, assign) id<AnyProtocol> delegate;

现在,当我尝试合成时,我收到以下错误消息:

error: Automatic Reference Counting Issue: Existing ivar 'delegate' for unsafe_unretained property 'delegate' must be __unsafe_unretained

有什么想法吗?

【问题讨论】:

标签: iphone ios xcode xcode4 ios5


【解决方案1】:

这个错误是因为ARC下的ivars默认为strong

这个错误告诉你,你已经声明了一个具有__unsafe_unretained(分配)所有权的属性,但默认情况下,ivar 拥有__strong 所有权,所以它们不能合二为一。你可以

  1. 省略 ivar,它将自动创建
  2. 定义 ivar 以匹配您的(分配)属性声明:

    __unsafe_unretained id <FileListDelegate> delegate;
    
  3. 定义属性以匹配 ivar 的隐式 __strong 所有权:

     @property (weak) id <FileListDelegate> delegate;
    

this 线程中的用户 chrispix 的答案中无耻地复制了三个选项..Credit 去那里

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    相关资源
    最近更新 更多