Skip to content

Instantly share code, notes, and snippets.

@reejosamuel
Created October 23, 2017 15:31
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 reejosamuel/753f20eda535149ededa839c1a0568d5 to your computer and use it in GitHub Desktop.
Save reejosamuel/753f20eda535149ededa839c1a0568d5 to your computer and use it in GitHub Desktop.
read and write files
let file = "file.txt" //this is the file. we will write to and read from it
let text = "some text" //just a text
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let fileURL = dir.appendingPathComponent(file)
//writing
do {
try text.write(to: fileURL, atomically: false, encoding: .utf8)
}
catch {/* error handling here */}
//reading
do {
let text2 = try String(contentsOf: fileURL, encoding: .utf8)
}
catch {/* error handling here */}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment