2013-03-06 28 views
4

返回錯誤結果當我嘗試使用代碼波紋管其誤差不返回到創建一個文件夾,當我試圖創建一個已經存在的,而不是返回是一個文件夾:的NSFileManager的方法createDirectoryAtPath iOS上

[[NSFileManager defaultManager] createDirectoryAtPath:[documentsPath stringByAppendingPathComponent:@"temp"] withIntermediateDirectories:NO attributes:nil error:&error]; 

蘋果documentation說:

Return Value 
YES if the directory was created or already exists or NO if an error occurred. 

所以我應該得到是成功或者文件夾是否存在。但是,當文件夾存在我得到這個消息:

Error Domain=NSCocoaErrorDomain Code=516 "The operation couldn’t be completed. (Cocoa error 516.)" UserInfo=0x200ef5f0 {NSFilePath=/var/mobile/Applications/DA657A0E-785D-49B4-9258-DF9EBAC5D52A/Documents/temp, NSUnderlyingError=0x200ef590 "The operation couldn’t be completed. File exists"} 

這是一個錯誤,應當報蘋果還是我做錯了什麼?

+0

@trojanfoe:我確定。我已經重新安裝了應用程序。第一次創建「temp」文件夾,但所有後續嘗試都會給我帶來錯誤。 – 2013-03-06 10:52:26

回答

3

[NSFileManager createDirectoryAtPath:withIntermediateDirectories:attributes:error:]如果文件存在並且不是目錄,將會失敗。

所以,前進的道路是不打擾創建目錄,如果它已經存在,如果存在且不是目錄拋出異常:

NSString *filename = [documentsPath stringByAppendingPathComponent:@"temp"]; 
NSFileManager *fileman = [NSFileManager defaultManager]; 
BOOL isDir = NO; 
if (![fileman fileExistsAtPath:filename isDirectory:&isDir]) 
{ 
    // Create the directory 
} 
else if (!isDir) 
{ 
    NSLog(@"Cannot proceed!"); 
    // Throw exception 
} 
+0

如果我正確地理解了你的問題,那麼Apple的文檔或實現是錯誤的? – 2013-03-06 10:54:16

+0

@BorutTomazin是的,我猜是的;聽起來像一個bug,所以你需要解決它(我認爲最好避免做一些無論如何不需要做的事情)。 – trojanfoe 2013-03-06 10:56:36

+0

好的。很高興知道。我會向蘋果報告錯誤。謝謝! – 2013-03-06 10:58:14

0

我認爲你需要使用withIntermediateDirectories YES得到蘋果文檔中的行爲。

相關問題