Last active
August 23, 2018 02:48
-
-
Save jayz5/5851d42a15ff92751ac0b8129e85b1b8 to your computer and use it in GitHub Desktop.
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
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