委托的__unsafe_unretained将无法构建

时间:2012-04-09 21:47:40

标签: objective-c ios

我从.h文件中获取此代码(代码段):

#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;

.m文件如下所示:

#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];

我在SO上查找类似的情况,看到我需要在属性中放置“__unsafe_unretained”...我做了(希望是正确的),但它仍然在构建上失败。完整的错误消息是:具有assign属性的属性'delegate'的现有ivar'委托'必须是__unsafe_unretained

Screenshot

我做错了什么?

2 个答案:

答案 0 :(得分:34)

正如错误消息告诉您的那样, ivar

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

需要声明__unsafe_unretained

__unsafe_unretained id<ILHuePickerViewDelegate> delegate;

不是财产:

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

因为ARC所有权限定符不适用于属性;它们只适用于变量。

由于@synthesize指令为您创建了 ivar(使用正确的ARC限定符),但您可以跳过它的声明:

@interface ILHuePickerView : ILView 

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

// etc.

事实上,现在是推荐的程序;请参阅TOCPL中的Defining Classes

答案 1 :(得分:2)

我过去使用过ILColorPicker,它绝对不是ARC准备好的。组 ILColorPicker类的编译器标志设置中的-fno-objC-arc