我从.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”……我这样做(希望正确),但是在构建时仍然失败。完整的错误消息是:具有分配属性的属性“delegate”的现有ivar“delegate”必须为__unsafe_unretained
我究竟做错了什么?
最佳答案
当错误消息告诉您时,ivar:
@interface ILHuePickerView : ILView {
id<ILHuePickerViewDelegate> delegate; // <-- This is the ivar
需要声明
__unsafe_unretained
:__unsafe_unretained id<ILHuePickerViewDelegate> delegate;
不是属性(property):
@property (assign, nonatomic) IBOutlet id<ILHuePickerViewDelegate> delegate;
因为ARC所有权限定词不适用于属性(property);它们仅适用于变量。
但是,由于
@synthesize
指令为您创建了ivar(使用正确的ARC限定符),因此您可以跳过其声明:@interface ILHuePickerView : ILView
/**
* Delegate
*/
@property (assign, nonatomic) IBOutlet id<ILHuePickerViewDelegate> delegate;
// etc.
实际上,现在是推荐的程序;请参见TOCPL中的Defining Classes。
关于objective-c - __unsafe_unretained为代表将无法建立,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10080312/