我试图在这样的类别中添加一个 init 方法:
@interface NSError (message)
+(id)errorWithCode:(NSInteger)code message:(NSString*)message;
-(id)initWithCode:(NSInteger)code message:(NSString*)message;
@end
- @implementation NSError(消息)
+(id)errorWithCode:(NSInteger)code message:(NSString*)message;
{
return [[[[self class] alloc] initWithCode:code message:message] autorelease];
}
-(id)initWithCode:(NSInteger)code message:(NSString*)message;
{
NSMutableDictionary * userInfo = [NSMutableDictionary dictionary];
[userInfo setValue:message forKey:NSLocalizedDescriptionKey];
self = [super initWithDomain:@"some.domain" code:code userInfo:userInfo]; // problem line
return self;
}
@end
但它对类 NSObject 的“超级”beeing 并且不响应 initWithDomain 感到不满......我尝试将 super 转换为 NSError 但编译器说这是不允许的。
如果我运行它,我会得到“无法识别的选择器发送到...”所以这不仅仅是一个转换错误。