Read string from file

 Problem

I have a text file and want to read its contents into a String.



 Recipe


// Set the file path
let path = "myfile.txt"

do {
    // Get the contents
    let contents = try NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding)
    print(contents)
}
catch let error as NSError {
    print("Ooops! Something went wrong: \(error)")
}
				


 Playground