Swift: failing to copy files to a newly created folder

后端 未结 1 1770
情歌与酒
情歌与酒 2020-12-01 19:54

I\'m building a simple program in Swift, it should copy files with a certain extension to a different folder. If this folder exist the program will just copy them in the fol

相关标签:
1条回答
  • 2020-12-01 20:34

    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 (code updated for Swift 3 and later)

    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)
    }
    
    0 讨论(0)
提交回复
热议问题