27
votes

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
I was able to get rid of my error by changing the line where I synthesize it to: @synthesize itemDelegate = _itemDelegate; to the effect where I call _itemDelegate in my methods now. But does anyone have an explanation for this or a different solution? Thanks again.Dylan Reich
For the modern runtime (iOS 4.0 or later that supports ARC), you don't need to declare ivars at all for properties.Kazuki Sakamoto
I am aware that I don't have to declare it (you can just set the property) but I would like to be able to learn "the old way", if you understand me.Dylan Reich

3 Answers

46
votes

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.

9
votes

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
1
votes

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.