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)")
}
				

Heard the news?

The Swift Cookbook for Swift 3 is at last here! For iBooks, Kindle and Paperback.
Hurry up - and Check it out! ;-)

 

 Playground