【问题标题】:__unsafe_unretained for a delegate won't build代表的 __unsafe_unretained 不会构建
【发布时间】:2012-04-09 21:47:40
【问题描述】:

我有这个来自 .h 文件的代码 (sn-p):

#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 属性必须是 __unsafe_unretained

我做错了什么?

【问题讨论】:

  • 因为这是个好问题,我投了赞成票。

标签: objective-c ios


【解决方案1】:

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

【讨论】:

  • 谢谢...我尝试评论@property,但没有成功,所以我只是在声明中添加了__unsafe_unretained...清理了该错误的构建...谢谢.
【解决方案2】:

我过去使用过 ILColorPicker,但它绝对不是 ARC 就绪的。放 -fno-objC-arc 在 ILColorPicker 类的编译器标志设置中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 2012-01-25
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 2023-01-28
    • 1970-01-01
    相关资源
    最近更新 更多