Getting "Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file" unexpectedly while attempting to create a small log file. Here's some background.
This is a Swift app I wrote for my own use six years ago.
A week ago, I made a small update to the app, which has not been changed in over two years. First time using Xcode 16.4 on this app, which required some code updates of course. The code creating this file has not been changed.
Now for the first time, I'm getting this permissions error on a folder with wide-open permissions.
This is the code. Worked for years under previous versions of Xcode.
*
if let outputURL = URL(string: "file://" + logPath + "/output_" + outputFormatter.string(from:Date()) + ".txt"){
do{
try outputString.write(to: outputURL, atomically:false, encoding: .utf8)
}catch let error as NSError{
print ("log write error (error) (nl) (outputString)")
}
}
Fixed it after some extensive testing. Nothing to do with Xcode or Sandbox.
Everything to do with logPath.
The folder location was all wrong. I did a correct update based on a version that was never implemented, so an error snuck into the process.
Took some time to get this right. Now it's compiling without warnings and running reliably again.