简体   繁体   中英

ARC error when declaring delegate ivar

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

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM