Skip to content

Instantly share code, notes, and snippets.

@jayz5
Last active August 23, 2018 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayz5/5851d42a15ff92751ac0b8129e85b1b8 to your computer and use it in GitHub Desktop.
Save jayz5/5851d42a15ff92751ac0b8129e85b1b8 to your computer and use it in GitHub Desktop.
func createDocFile(fileName: String, content: String) {
// get the document directory url
guard let docDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
// choose the location of the file to be withinthe document directory
let fileURL = docDir.appendingPathComponent(fileName)
print("file url: ", fileURL)
do {
// creating or appending to a file by writing to the file url
print("writing data to file...")
try content.write(to: fileURL, atomically: true, encoding: .utf8)
// instantiate the contents of a file into an appropriate type
let newString = try String(contentsOf: fileURL)
print("new string: ", newString)
} catch {
print("data writing error \(error)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment