2012-06-13 19 views
0

我有一個使用ARC和接受一個NSError指針的方法和我傳遞到的NSFileManagercontentsOfDirectoryAtPath:error:方法如下所示:Objective-C的傳遞NSError指針的NSFileManager定製方法內

+ (NSArray *) getContentsOfCurrentDirectoryWithError: (NSError **)error 
{ 
    // code here 

    _contentsOfCurrentDirectory = [_fileManager contentsOfDirectoryAtPath: _documentDirectory error: error]; 

    // more code here 
} 

我不當然,如果這段代碼是正確的,因爲我不習慣指向由於被託管語言所損壞的指針。然而,我最初的反應是這樣做的:

_contentsOfCurrentDirectory = [_fileManager contentsOfDirectoryAtPath: _documentDirectory error: &error]; 

Xcode大吼大叫,因爲試圖這樣,雖然。我關於這可能如何工作的假設是contentsOfDirectoryAtPath:error:方法要求一個指針,並且因爲我在getContentsOfCurrentDirectoryWithError:error:方法中要求指針,所以我可以在不使用解引用運算符的情況下通過error

我只是想確保我這樣做是爲了避免後來的麻煩,那麼我有什麼問題嗎?

回答

0

方法可以是這樣的NSError **在h文件的errorMessage

+ (NSArray *) getContentsOfCurrentDirectory 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *_documentsDirectory = [paths objectAtIndex:0]; 
    NSArray *_contentsOfCurrentDirectory = [[NSFileManager defaultManger] contentsOfDirectoryAtPath: _documentDirectory error: errorMessage]; 
    return contentsOfCurrentDirectory ; 
} 

每當錯誤發生時的errorMessage 參考將有錯誤消息[錯誤說明]

+0

好,我曾打算因爲我的自定義方法能夠暴露內部方法可能遇到的任何錯誤。 –

+0

謝謝,這有幫助。 –