ARC error when declaring delegate ivar

2019-02-07 18:10发布

I am using ARC (no, this is not NDA). I am declaring my ivar in my interface with

id itemDelegate;

I then declare the property:

@property (nonatomic, weak) id<mySecretDelegateYouAreNotSupposedToSeeOnSO> itemDelegate; (with weak instead of assign because of ARC)

In my implementation file I simply synthesize it: @synthesize itemDelegate;

However, I am getting the error:

"Existing ivar 'ItemDelegate' for _weak property 'itemDelegate' must be _weak".

Anyone know what's wrong? Thanks for your help.

ARC - Automatic Reference Counting

3条回答
Fickle 薄情
2楼-- · 2019-02-07 18:36

Try something like the following (example from: http://vinceyuan.blogspot.com/2011/06/wwdc2011-session-323-introducing.html):

@interface SomeObject : NSObject {
   __weak id <SomeObjectDelegate> delegate;
}
@property (weak) id <SomeObjectDelegate> delegate;
@end

Please notice how the ivar is declared.

查看更多
劫难
3楼-- · 2019-02-07 18:36

There is an issue where, even if you update XCode (4.2+) from the Mac App Store, as Apple requires, it leaves the old version of XCode on your computer. So, if you had XCode pinned to your launchpad, and launch it, you'll get all these errors as noted below. You have to find the newer version of XCode, say by using the Spotlight feature, run it, and then as one of its first tasks, it removes the old version of XCode. Then you have no more errors reporting like this.

查看更多
冷血范
4楼-- · 2019-02-07 18:45

With ARC and iPhone Simulator 5.0 the following seems to work just fine (no warnings, etc...):

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

SomeObject.m

#import "SomeObject.h"

@implementation SomeObject
@synthesize delegate = _delegate;
@end
查看更多
登录 后发表回答