建议使用ARC声明委托属性的方法

cfi*_*her 31 cocoa-touch delegates objective-c automatic-ref-counting

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

@property (assign) id<FooDelegate> delegate;
Run Code Online (Sandbox Code Playgroud)

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

@property (weak) id<FooDelegate> delegate;
Run Code Online (Sandbox Code Playgroud)

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

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

djr*_*ero 23

Xcode 4 Refactor > 转换为Objective-C ARC转换:

@interface XYZ : NSObject
{
    id delegate;
}
@property (assign) id delegate;
...
@synthesize delegate;
Run Code Online (Sandbox Code Playgroud)

成:

@interface XYZ : NSObject
{
    id __unsafe_unretained delegate;
}
@property (unsafe_unretained) id delegate;
...
@synthesize delegate;
Run Code Online (Sandbox Code Playgroud)

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

  • 这是相关的*2*年前,当一个年轻的clang和老式iOS SDK填充地球. (2认同)

Jan*_*ano 22

__unsafe_unretained而是weak针对iOS 4和5的ARC项目使用.唯一的区别是weak在解除分配时nils指针,并且它仅在iOS 5中受支持.

你的另一个问题是在为什么Objective-C代表通常给予属性赋值而不是保留?.