对于委托的__unsafe_unretained将无法构建 [英] __unsafe_unretained for a delegate won't build
问题描述
我从.h文件获得此代码(代码段):
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;
.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
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
我做错了什么?
推荐答案
正如错误消息告诉您的那样, ivar :
As the error message is telling you, the 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所有权限定符不适用于属性;它们只适用于变量。
because the ARC ownership qualifiers don't apply to properties; they only apply to variables.
由于 @synthesize
指令创建了 ivar for你(使用正确的ARC限定符),但是,你可以跳过它的声明:
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.
这篇关于对于委托的__unsafe_unretained将无法构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!