I have Googled and poked around Stack Overflow and can't seem to find a solution for this. I have:
let fileURL = URL( string: "file:///Users/me/file.txt" )
var rawDataString: String
var errorString: String?
do {
rawDataString = try String( contentsOf: fileURL!, encoding: String.Encoding.utf8 )
} catch let error as NSError {
errorString = error.description
print( errorString! )
return
}
and it's erroring out with
Error Domain=NSCocoaErrorDomain Code=257 "The file “file.txt” couldn’t be opened because you don’t have permission to view it."
Permissions are read for all users:
$ ls -al file.txt
-rw-r--r--@ 1 me staff 348306 Dec 13 2016 file.txt
Any ideas would be most welcome.
Anyone coming across this thread, @LeoDabus pointed me to where to turn off sandbox, which worked:
He also cleaned up my code a bit:
let fileURL = URL( fileURLWithPath: "/Users/me/file.txt" )
var rawDataString: String
var errorString: String?
do {
rawDataString = try String( contentsOf: fileURL, encoding: .utf8 )
} catch let error as NSError {
errorString = error.description
rawDataString = ""
return
}
I actually was not able to get the preferred answers above to work for me in swift playground.
Another solution is just to create a command line
app in Xcode. Then paste the above and it should work fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With