📜  replaceItem(at dstURL: URL, with srcURL: URL) - 任何代码示例

📅  最后修改于: 2022-03-11 14:58:55.343000             🧑  作者: Mango

代码示例1
let fileManager = FileManager.default

func copyItem(at srcURL: URL, to dstURL: URL) {
    do {
        try fileManager.copyItem(at: srcURL, to: dstURL)
    } catch let error as NSError {
        if error.code == NSFileWriteFileExistsError {
            print("File exists. Trying to replace")
            replaceItem(at: dstURL, with: srcURL)
        }
    }
}

func replaceItem(at dstURL: URL, with srcURL: URL) {
    do {
        try fileManager.removeItem(at: dstURL)
        copyItem(at: srcURL, to: dstURL)
    } catch let error as NSError {
        print(error.localizedDescription)
    }
}