2012-05-08 101 views
1

我試着去添加某一類別中的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 

但complais關於 「超級」 beeing類NSObject和沒有響應initWithDomain ... 我試圖超級NSError投入,但編譯器說不允許。

如果我運行它,我得到「無法識別的選擇器發送到...」,所以它不只是一個鑄造錯誤。

回答

3

您從NSError沒有繼承,你添加新的代碼相同的類。所以,而不是super你應該可以打電話self

+0

哦,當然,謝謝一堆! – oskob

+0

但是如果你用自己替換超級,如果自己還沒有完全初始化,那麼它是如何工作的? – avance

+0

這與您有一個指定的初始化程序和一堆方便初始程序的類相同。我在猜測,但我會想象''alloc'爲'self'設置足夠的信息以在這種情況下工作。 –