Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__unsafe_unretained for a delegate won't build

I have this code (snippet) from a .h file:

#import <UIKit/UIKit.h>
#import "ILView.h"

/**
 * Controls the orientation of the picker
 */
typedef enum {
    ILHuePickerViewOrientationHorizontal     =   0,
    ILHuePickerViewOrientationVertical       =   1
} ILHuePickerViewOrientation;

@class ILHuePickerView;

/**
 * Hue picker delegate
 */
@protocol ILHuePickerViewDelegate

/**
 * Called when the user picks a new hue
 *
 * @param hue 0..1 The hue the user picked
 * @param picker The picker used
 */
-(void)huePicked:(float)hue picker:(ILHuePickerView *)picker;

@end

/**
 * Displays a gradient allowing the user to select a hue
 */
@interface ILHuePickerView : ILView {
    id<ILHuePickerViewDelegate> delegate;
    float hue;
    ILHuePickerViewOrientation pickerOrientation;
}

/**
 * Delegate
 */
//@property (assign, nonatomic) IBOutlet id<ILHuePickerViewDelegate> delegate;
@property (assign, nonatomic) IBOutlet __unsafe_unretained id<ILHuePickerViewDelegate> delegate;

/**
 * The current hue
 */
@property (assign, nonatomic) float hue;

The .m file looks like this:

#import "ILHuePickerView.h"
#import "UIColor+GetHSB.h"

@interface ILHuePickerView(Private)

-(void)handleTouches:(NSSet *)touches withEvent:(UIEvent *)event;

@end

@implementation ILHuePickerView

@synthesize color, delegate, hue, pickerOrientation;

#pragma mark - Setup

-(void)setup
{
    [super setup];

I looked on SO for similar cases, and saw that I needed to put "__unsafe_unretained" in the property... I did that (hopefully correct), but it still fails on the build. The full error message is: Existing ivar 'delegate' for property 'delegate' with assign attribute must be __unsafe_unretained

Screenshot

What am I doing wrong?

like image 620
SpokaneDude Avatar asked Apr 09 '12 21:04

SpokaneDude


2 Answers

As the error message is telling you, the ivar:

@interface ILHuePickerView : ILView {
    id<ILHuePickerViewDelegate> delegate;    // <-- This is the ivar

needs to be declared __unsafe_unretained:

__unsafe_unretained id<ILHuePickerViewDelegate> delegate;

not the property:

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

because the ARC ownership qualifiers don't apply to properties; they only apply to variables.

Since the @synthesize directive creates the ivar for you (with the correct ARC qualifier), however, you can just skip its declaration:

@interface ILHuePickerView : ILView 

/**
 * Delegate
 */
@property (assign, nonatomic) IBOutlet id<ILHuePickerViewDelegate> delegate;

// etc.

Which is, in fact, now the recommended procedure; see Defining Classes in TOCPL.

like image 142
jscs Avatar answered Sep 20 '22 03:09

jscs


I've used ILColorPicker in the past, and it is definitely not ARC ready. Set -fno-objC-arc in the compiler flag settings for the ILColorPicker classes.

like image 30
CodaFi Avatar answered Sep 20 '22 03:09

CodaFi