声明委托ivar时出现ARC错误

Dyl*_*ich 27 iphone ios automatic-ref-counting

我正在使用ARC(不,这不是NDA).我在我的界面中宣布我的ivar

id itemDelegate;
Run Code Online (Sandbox Code Playgroud)

然后我宣布财产:

@property (nonatomic, weak) id<mySecretDelegateYouAreNotSupposedToSeeOnSO> itemDelegate; (由于ARC而弱而不是分配)

在我的实现文件中,我只是合成它: @synthesize itemDelegate;

但是,我收到错误:

"Existing ivar 'ItemDelegate' for _weak property 'itemDelegate' must be _weak".
Run Code Online (Sandbox Code Playgroud)

谁知道什么是错的?谢谢你的帮助.

ARC - 自动参考计数

Wol*_*urs 46

尝试类似下面的内容(例如:http://vinceyuan.blogspot.com/2011/06/wwdc2011-session-323-introducing.html):

@interface SomeObject : NSObject {
   __weak id <SomeObjectDelegate> delegate;
}
@property (weak) id <SomeObjectDelegate> delegate;
@end
Run Code Online (Sandbox Code Playgroud)

请注意ivar是如何申报的.

  • = WOLFGANG =重要注意事项:虽然你是正确的,id-s是没有\*写的,但不是因为"id是NSObject\*的简写" - 一个id绝对不是NSObject*(尽管分配给它的对象)很可能是.)UnixJunkie在这里有一个很好的解释:http://unixjunkie.blogspot.com/2008/03/id-vs-nsobject-vs-id.html (6认同)

Wol*_*urs 9

使用ARC和iPhone模拟器5.0,以下似乎工作正常(没有警告等...):

SomeObject.h

@class SomeObject;
@protocol SomeObjectDelegate <NSObject>
- (void)someObjectDidFinishDoingSomethingUseful:(SomeObject *)object;
@end


@interface SomeObject : NSObject {
   __unsafe_unretained id <SomeObjectDelegate> _delegate;
}
@property (nonatomic, assign) id <SomeObjectDelegate> delegate;
@end
Run Code Online (Sandbox Code Playgroud)

SomeObject.m

#import "SomeObject.h"

@implementation SomeObject
@synthesize delegate = _delegate;
@end
Run Code Online (Sandbox Code Playgroud)