ARC error when declaring delegate ivar

有些话、适合烂在心里 提交于 2019-12-03 19:44:23

问题


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


回答1:


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.




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/7021852/arc-error-when-declaring-delegate-ivar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!