Created
May 14, 2018 23:01
Star
You must be signed in to star a gist
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
// Save data to file | |
let fileName = "Test" | |
let DocumentDirURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) | |
let fileURL = DocumentDirURL.appendingPathComponent(fileName).appendingPathExtension("txt") | |
print("FilePath: \(fileURL.path)") | |
let writeString = "Write this text to the fileURL as text in iOS using Swift" | |
do { | |
// Write to the file | |
try writeString.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8) | |
} catch let error as NSError { | |
print("Failed writing to URL: \(fileURL), Error: " + error.localizedDescription) | |
} | |
var readString = "" // Used to store the file contents | |
do { | |
// Read the file contents | |
readString = try String(contentsOf: fileURL) | |
} catch let error as NSError { | |
print("Failed reading from URL: \(fileURL), Error: " + error.localizedDescription) | |
} | |
print("File Text: \(readString)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment