Skip to content

Instantly share code, notes, and snippets.

@andrewwoz
Created October 16, 2019 14:31
Show Gist options
  • Select an option

  • Save andrewwoz/3fda6be2560cd9edd0b6b3b6fd0a4257 to your computer and use it in GitHub Desktop.

Select an option

Save andrewwoz/3fda6be2560cd9edd0b6b3b6fd0a4257 to your computer and use it in GitHub Desktop.
[Replace item(file)] #fs
let fs = FileManager.default
let docsURL = ...
// create first test file
let newFile = docs.appendingPathComponent("newFile.txt")
fs.createFile(
atPath: newFile.path,
contents: ...,
attributes: nil)
// create second test file
let fileToReplace = docs.appendingPathComponent("fileToReplace.txt")
fs.createFile(
atPath: fileToReplace.path,
contents: ...,
attributes: nil)
try? fs.replaceItem(
at: fileToReplace,
withItemAt: newFile,
backupItemName: "contentOfOldFile.txt",
options: .withoutDeletingBackupItem,
resultingItemURL: nil
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment