ios - 重写init方法throws方法实现与其声明不匹配

时间:2014-12-01 11:20:11

标签: ios objective-c constructor

尝试在类中覆盖init方法(以创建具有已初始化标记的实例)并获取异常。代码示例:

@interface DiagnosticsReport : NSObject  {
}
@property NSString *tag;
- (void) initWithTag:(NSString*) tag;
@end

@implementation DiagnosticsReport
- (id) initWithTag:(NSString*) tag {
    if (self = [self init]) {
        _tag = tag;
    }
    return self;
}

- (id) init {
    if (self = [super init]) {
        // default init here
    }
    return self;
}

1 个答案:

答案 0 :(得分:1)

您的方法声明返回void,而定义返回id。将两者都更改为返回instancetype,您就可以了。