Objective c 如何设置代理的默认值?

Objective c 如何设置代理的默认值?,objective-c,automatic-ref-counting,delegation,Objective C,Automatic Ref Counting,Delegation,我有一个类,它具有符合协议的委托属性: MyClass.h @interface MyClass : NSObject @property (nonatomic, weak) id<MyProtocol> delegate; @end 这种方法是否存在任何问题,或者是否可以以任何方式加以改进 更新#1 下面呢 该属性不是名为委托,而是类似于颜色调色板或字体系列的属性,需要一个默认值 协议中的所有内容都是@必需的 在运行时,属性的值可以设置为其他值(符合协议的另一个类的实例)

我有一个类,它具有符合协议的
委托
属性:

MyClass.h

@interface MyClass : NSObject

@property (nonatomic, weak) id<MyProtocol> delegate;

@end
这种方法是否存在任何问题,或者是否可以以任何方式加以改进


更新#1 下面呢

该属性不是名为
委托
,而是类似于
颜色调色板
字体系列
的属性,需要一个默认值

协议中的所有内容都是
@必需的

在运行时,属性的值可以设置为其他值(符合协议的另一个类的实例)


更新#2 我试图封装(默认)行为


考虑到有3个类符合协议-最初我可能希望使用第一个类中的所有值。明天我可能会选择第二个更好的默认值。此外,用户可以在运行时设置一个首选项,该首选项可以使用3个选项中的任何一个。

默认值应为零。没有理由给它一个值。当所有其他类都使用它时,预期的行为是需要完全实现委托。当您开始添加默认值时,您正在挫败整个要点

默认情况下,您的委托应为零。但是,为了实现UITableView之类的默认值,您的类应该检查委托是否使用响应每个可选委托方法(如果委托为nil,它将响应false)。如果代理没有响应,则使用默认值。您无需设置“默认代理”即可完成此操作

您的代码如下所示:

NSString *stringValue = nil;
if ([self.delegate respondsToSelector:@selector(getStringValue)]) {
     stringValue = [self.delegate getStringValue];
}
else {
    stringValue = @"Default value";
}

您可能正在寻找这种实现:

if (delegate == nil) {
    // default behavior
} else {
    [delegate methodToCallWithParameters:...];
}

如果您的酒店必须实施协议,这是一个类似的问题。

基于一系列响应,听起来我应该这样做:

@interface MyClass ()

@property (nonatomic, strong) id<MyProtocol> currentColorPalette;
@property (nonatomic, strong) id<MyProtocol> defaultColorPalette;

@end


@implementation MyClass

- (instancetype)init
{
    self = [super init];

    if (self) {
        _defaultColorPalette = [[SomeClassConformingToProtocol alloc] init];
    }

    return self;
}

- (void)setColorPalette:(id<MyProtocol>)colorPalette
{
    _colorPalette = colorPalette;
    _currentColorPalette = _colorPalette;
}

- (id<MyProtocol>)currentColorPalette
{
    id<MyProtocol> colorPalette = nil;

    if (self.currentColorPalette == nil) {
        colorPalette = self.defaultColorPalette;
    }
    else {
        colorPalette = self.currentColorPalette;
    }

    return colorPalette;
}


#pragma mark - MyProtocol

- (void)doSomethingWithCurrentColorPalette
{
    [self.currentColorPalette someDelegateMethod];
}

@end
@接口MyClass()
@属性(非原子,强)id currentColorPalette;
@属性(非原子,强)id defaultColorPalette;
@结束
@MyClass的实现
-(instancetype)初始化
{
self=[super init];
如果(自我){
_defaultColorPalette=[[SomeClassConformingToProtocol alloc]init];
}
回归自我;
}
-(void)setColorPalette:(id)colorPalette
{
_颜色调色板=颜色调色板;
_currentColorPalette=\u colorPalette;
}
-(id)当前颜色调色板
{
id colorpolete=nil;
if(self.currentColorPalette==nil){
colorPalette=self.defaultColorPalette;
}
否则{
colorPalette=self.currentColorPalette;
}
返回调色板;
}
#pragma-mark-MyProtocol
-(无效)使用CurrentColorPalette的DoSomething
{
[self.currentColorPalette someDelegateMethod];
}
@结束

nil
因为向
nil
发送消息也是不允许的,所以约定委托的默认值应该是nil。因此,无论您在何处对委托调用方法,都要确保其行为正常。如果设置委托值不是可选的,只需在协议值为nil时断言即可。委托通常是弱(赋值)引用,而“非”强(保留)。它的默认值为零。您可以在未设置委托的位置实现逻辑。
@interface MyClass ()

@property (nonatomic, strong) id<MyProtocol> currentColorPalette;
@property (nonatomic, strong) id<MyProtocol> defaultColorPalette;

@end


@implementation MyClass

- (instancetype)init
{
    self = [super init];

    if (self) {
        _defaultColorPalette = [[SomeClassConformingToProtocol alloc] init];
    }

    return self;
}

- (void)setColorPalette:(id<MyProtocol>)colorPalette
{
    _colorPalette = colorPalette;
    _currentColorPalette = _colorPalette;
}

- (id<MyProtocol>)currentColorPalette
{
    id<MyProtocol> colorPalette = nil;

    if (self.currentColorPalette == nil) {
        colorPalette = self.defaultColorPalette;
    }
    else {
        colorPalette = self.currentColorPalette;
    }

    return colorPalette;
}


#pragma mark - MyProtocol

- (void)doSomethingWithCurrentColorPalette
{
    [self.currentColorPalette someDelegateMethod];
}

@end