2010-04-07 80 views
0

我在過去的4個小時裏一直在摸頭,嘗試各種各樣的小實驗,但似乎無法弄清楚發生了什麼問題。這可能是一個編譯器錯誤?通過NSError **作爲方法參數時的編譯器警告

Test.m:

- (id)initWithContentsOfURL:(NSURL *)aURL error:(NSError **)error 
{ 
    if (!(self = [super init])) { 
     return nil; 
    } 
    return self; 
} 

的main.m:

NSError *error; 

Test *t = [[Test alloc] initWithContentsOfURL:[NSURL fileURLWithPath:@"/"] error:&error]; 

這裏的編譯器(從main.m文件)警告:

warning: incompatible Objective-C types 'struct NSError **', expected 'struct NSDictionary **' when passing argument 2 of 'initWithContentsOfURL:error:' from distinct Objective-C type

我使用的是最新的Xcode和Snow Leopard的版本。

回答

5

我懷疑它正在選取一個不同的選擇器實例initWithContentsOfURL:error: - 也許是NSAppleScript中的一個實例。請記住,[NSObject alloc]返回id

您的代碼在運行時是否按預期工作?

試着將[Test alloc]歸還Test*


Test *t = [(Test*)[Test alloc] initWithContentsOfURL:[NSURL fileURLWithPath:@"/"] error:&error]; 
+3

你是正確的。 Objective-C不喜歡具有相同簽名的方法具有不同的類型(即它不會重載),因此如果兩個方法採用不同的參數,請避免將兩個方法命名爲相同。 – 2010-04-07 11:09:12

+0

就是這樣!謝謝!我不知道Objective-C可能遇到這些命名空間污染問題。你會認爲蘋果會在Objective-C 2.0中解決這個問題...... – splicer 2010-04-07 11:18:45

+1

是的,這是一個令人討厭的問題。我被它咬了一陣子:http://stackoverflow.com/questions/312608/why-do-i-need-to-cast-self-to-id – philsquared 2010-04-07 11:20:23