Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Existing ivar 'delegate' for unsafe_unretained property 'delegate' must be __unsafe_unretained

Tags:

objective-c

I'm getting the error above, but unsure how to go about fixing it. This is my code:

.h:

#import <UIKit/UIKit.h>

@protocol ColorLineDelegate <NSObject>

-(void)valueWasChangedToHue:(float)hue;

@end

@interface ColorLine : UIButton {

    id <ColorLineDelegate> delegate;

}

@property (nonatomic, assign) id <ColorLineDelegate> delegate;

@end

.m:

#import "ColorLine.h"

@implementation ColorLine

@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

@end

The error occurs in the synthesize line. I can't find a problem though.

like image 285
Andrew Avatar asked Nov 15 '11 15:11

Andrew


3 Answers

Use this syntax:

@interface SomeClass  : NSObject {
    id <SomeClassDelegate> __unsafe_unretained  delegate;
}
@property (unsafe_unretained) id <SomeClassDelegate> delegate;
like image 155
janBP Avatar answered Nov 13 '22 10:11

janBP


Looks like your project might be using ARC then properties should be declared this way:

#import <UIKit/UIKit.h>

@protocol ColorLineDelegate <NSObject>
-(void)valueWasChangedToHue:(float)hue;
@end

@interface ColorLine : UIButton 
@property (nonatomic, weak) id <ColorLineDelegate> delegate;
@end
like image 52
rckoenes Avatar answered Nov 13 '22 09:11

rckoenes


I had the same problem when I used old example code which did not feature ARC in my ARC project. It seems that you do not need to put the variable declarations into the interface definition any more. So your code should work like this:

h:

#import <UIKit/UIKit.h>

@protocol ColorLineDelegate <NSObject>

-(void)valueWasChangedToHue:(float)hue;

@end

@interface ColorLine : UIButton {

    // Get rid of this guy!
    //id <ColorLineDelegate> delegate;
}

@property (nonatomic, assign) id <ColorLineDelegate> delegate;

@end

.m:

#import "ColorLine.h"

@implementation ColorLine

@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

@end
like image 21
Johannes Avatar answered Nov 13 '22 09:11

Johannes