objective-c - 使用 ARC 声明委托(delegate)属性的推荐方法

标签 objective-c cocoa-touch delegates automatic-ref-counting

我曾经将所有委托(delegate)属性声明为

@property (assign) id<FooDelegate> delegate;

我的印象是所有赋值属性现在都应该是弱指针,这是正确的吗? 如果我尝试声明为:

@property (weak) id<FooDelegate> delegate;

我在尝试 @synthesize 时遇到错误(不支持自动生成的弱属性)。

在这种情况下,最佳做法是什么?

最佳答案

Xcode 4 重构> 转换为 Objective-C ARC 转换:

@interface XYZ : NSObject
{
    id delegate;
}
@property (assign) id delegate;
...
@synthesize delegate;

进入:

@interface XYZ : NSObject
{
    id __unsafe_unretained delegate;
}
@property (unsafe_unretained) id delegate;
...
@synthesize delegate;

如果我没记错的话,在 WWDC 2011 视频中也提到了 ARC。

关于objective-c - 使用 ARC 声明委托(delegate)属性的推荐方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7753841/

相关文章:

objective-c - UINavigationBar 中的 UISearchBar 在 UIViewControllers 之间共享

iphone - 我在 json 中发送字典,它有两个数组和多个值,但我收到响应访问被拒绝

iphone - 原生iPhone数据库,iPhone上的所有数据

swift - 为什么在使用 TableView 委托(delegate)时会出现索引超出范围的 fatal error ?

iphone - 键盘打开时在 ScrollView 中滚动(就像在 Notes 应用程序 iOS 中一样)

objective-c - 如何使用 NSDateFormatter 将此字符串解析为 NSDate?

c# - 委托(delegate) P/Invoke 传递的实例方法

c# - 使用委托(delegate)来避免子类化

iphone - MGTwitterEngine 和 iPhone

iphone - UIScrollView动态调整UILabel的大小