gpt4 book ai didi

objective-c - __unsafe_unretained 代表不会建立

转载 作者:技术小花猫 更新时间:2023-10-29 10:13:36 26 4
gpt4 key购买 nike

我有来自 .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”……我做到了(希望是正确的),但它仍然在构建时失败。完整的错误信息是: Existing ivar 'delegate' for property 'delegate' with assign attribute must be __unsafe_unretained

Screenshot

我做错了什么?

最佳答案

正如错误消息告诉您的那样,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.

事实上,这是现在推荐的程序;见Defining Classes在 TOCPL 中。

关于objective-c - __unsafe_unretained 代表不会建立,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10080312/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com