2013-02-13 12 views
1

我有簡單的代碼:CreateDirectoryAtPath:不工作

NSFileManager *fileManager = [NSFileManager defaultManager]; 

if (!fileManager) NSLog(@"Manager doesn't exist!"); 

if(![fileManager fileExistsAtPath:destString]) { 
    if (![fileManager createDirectoryAtPath:destString withIntermediateDirectories:YES attributes:nil error:&error]){ 
     NSLog(@"%@", [error localizedFailureReason]); 
    } 
} 
else NSLog(@"Exists!"); 

瓦爾:

destString = file://localhost/Users/SOMEUSER/Desktop/NEWFOLDER/ 

當我試圖創建一個文件夾,然後程序寫入「存在」,但在桌面上不存在。當我刪除fileExistsAtPath時:那麼沒有錯誤,但沒有目錄。 Thx 4回覆!

回答

4

-createDirectoryAtPath:withIntermediateDirectories:attributes:error:將路徑創建爲UNIX樣式的路徑字符串,而不是文件URL樣式的字符串。也就是說,你想傳遞一個字符串,如/Users/SOMEUSER/Desktop/NEWFOLDER/

或者,如果您要處理的是URL樣式的字符串,則可以改爲使用-createDirectoryAtURL:withIntermediateDirectories:attributes:error:來代替,並從字符串構造NSURL

+0

問題已解決!謝謝bdash! – 2013-02-13 10:01:17