I'm trying to read a file which exist with the below code in Swift 2. It seems to be failing. Appreciate any inputs:
let someText = NSString(string:"some text")
let destinationPath = "/users/johnt/myFile.txt"
var filemgr = NSFileManager.defaultManager()
if filemgr.fileExistsAtPath(destinationPath) {
print("File exists")
do {
let readFile = try String(contentsOfFile: destinationPath, encoding: NSUTF8StringEncoding) // Throws this error
// How do I read the file contents from here?
print("\(readFile)")
} catch let error as NSError {
print("Error: \(error.domain)")
}
} else {
print("File does not exist")
do {
try someText.writeToFile(destinationPath, atomically: true, encoding: NSUTF8StringEncoding)
} catch let error as NSError {
print("Error: \(error.domain)")
}
}