1

I touched a file in my home directory, myFile.txt, and changed its permissions.

touch ~/myFile.txt
chmod a+rwx ~/myFile.txt

I'd like to write to this file from the XCode Playground,

import Cocoa
let someText = NSString( string:"some text" )
let destinationPath = "/Users/connorjohnson/myFile.txt"
var filemgr = NSFileManager.defaultManager()
do {
    try someText.writeToFile(destinationPath, atomically: true, encoding: NSUTF8StringEncoding)
} catch let error as NSError {
    print("Error: \(error)")
}

But I keep getting this error:

"Error: Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “myFile.txt” in the folder “connorjohnson”." UserInfo={NSFilePath=/Users/connorjohnson/myFile.txt, NSUnderlyingError=0x7f91e9725890 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}\n"

Is there a way to write to a file in my home directory? Or is this a built-in security feature? I compiled a Swift2 script to write to a file in my home directory and it worked just fine. Thanks!

cjohnson318
  • 3,154
  • 30
  • 33
  • Playgrounds are Sandboxed. You can not write to the filesystem from a Playground, but you can [read](http://stackoverflow.com/questions/31840335/swift-2-0-code-works-in-xcode-but-not-in-playground/31840630#31840630) a file from their Resources folder (you cannot write to this file from code, though). – Eric Aya Sep 16 '15 at 19:13
  • Playgrounds only have access to its document folder. You can get the path by `NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)` – David Skrundz Sep 16 '15 at 19:19

0 Answers0