Answers for "replaceItem(at dstURL: URL, with srcURL: URL)"

0

replaceItem(at dstURL: URL, with srcURL: URL)

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)
    }
}
Posted by: Guest on June-25-2021

Browse Popular Code Answers by Language