Read string from file


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


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

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


Find answers here

Hi there! Ask Us a Question We are ready to help