2011-05-09 107 views
2

我試圖將文件從路徑移動到另一個路徑的目錄。我手上有代碼。但是,它不能正常工作。我不知道那個代碼有什麼問題。iPhone sdk - 將文件移動到目錄

if ([[NSFileManager defaultManager] moveItemAtPath:"path to be moved" toPath:"destination path" error:&error]) { 
    NSLog(@"Moved"); 
} else { 
    NSLog(@"Not moved:%@", [error localizedDescription]); 
} 

實際上,如果移動過程成功,將會打印「移動」。但是結果打印錯誤信息「The operation could not be completed。(Cocoa error 512.)」。請幫我解決這個問題。提前致謝。

+0

嘗試copyItemAtPath:toPath:錯誤:不是moveItemAtPath:toPath:錯誤:, – visakh7 2011-05-09 10:19:04

+0

其實我我沒有從mainBundle移動文件。我必須將選定的文件從文檔目錄移動到選定的文件夾。 – iOS 2011-05-09 10:56:41

回答

2

使用下面的功能

[[NSFileManager defaultManager] copyItemAtPath:mainBundleFilePath 
             toPath:destPath 
             error:&err] 

詳細檢查下面SO後,

iPhone - copying a file from resources to Documents gives an error

+0

如果我使用'copyItemAtPath:',它會給出錯誤消息'操作無法完成。文件存在' – iOS 2011-05-09 10:52:33

+0

@Developer:看看下面所以SO帖子http://stackoverflow.com/questions/3639404/how-to-copy-a-file-to-portable-drive-root-with-cocoa – Jhaliya 2011-05-09 10:56:40

+0

謝謝非常。有效。 – iOS 2011-05-09 12:01:52

相關問題