Created
October 23, 2017 15:31
-
-
Save reejosamuel/753f20eda535149ededa839c1a0568d5 to your computer and use it in GitHub Desktop.
read and write files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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