Swift: failing to copy files to a newly created folder

 ̄綄美尐妖づ 提交于 2019-11-27 09:09:19

From the copyItemAtPath(...) documentation:

dstPath
The path at which to place the copy of srcPath. This path must include the name of the file or directory in its new location. ...

You have to append the file name to the destination directory for the copyItemAtPath() call.

Something like (untested):

let destPath = newMTSFolder.stringByAppendingPathComponent(element.lastPathComponent)
if NSFileManager.defaultManager().copyItemAtPath(fullElementPath, toPath: destPath, error: &err) {
     // ...

Update for Swift 3/4:

let srcURL = URL(fileURLWithPath: fullElementPath)
let destURL = URL(fileURLWithPath: newMTSFolder).appendingPathComponent(srcURL.lastPathComponent)

do {
    try FileManager.default.copyItem(at: srcURL, to: destURL)
} catch {
    print("copy failed:", error.localizedDescription)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!