How to writeToFile in .playground?

I edited the .plist file. As I understand it: because of the sandbox, you can read files inside the .playgrounds file in the Resources directory. But how can I write the edited file to this folder?

 // read file let xmlPath: String = NSBundle.mainBundle().pathForResource("Settings", ofType: "plist")! var xmlDictionary: [String : AnyObject] = NSDictionary(contentsOfFile: xmlPath) as Dictionary<String, AnyObject> // editing file // ... // saving file let saveDict: NSDictionary = xmlDictionary as NSDictionary let savePath: String = xmlPath.stringByDeletingLastPathComponent.stringByAppendingPathComponent("Saved.plist") saveDict.writeToFile(savePath, atomically: false) // ~> false 

Whether savePath and xmlPath , returns false , which does not mean success.

It would be nice if I could replace the old (non-editable) version of the .plist file, but saving to a new file would also be fine.

+5
source share
1 answer

Changing files in the main package is not possible, see the previous question . What you can do is save in folders with isolated applications, the URLs of which can be obtained as follows:

  func applicationDirectory(directory:NSSearchPathDirectory) -> NSURL? { var appDirectory:String? var paths:[AnyObject] = NSSearchPathForDirectoriesInDomains(directory, NSSearchPathDomainMask.UserDomainMask, true); if paths.count > 0 { if let pathString = paths[0] as? String { appDirectory = pathString } } if let dD = appDirectory { return NSURL(string:dD) } return nil } func applicationTemporaryDirectory() -> NSURL? { if let tD = NSTemporaryDirectory() { return NSURL(string:tD) } return nil } applicationTemporaryDirectory() applicationDirectory(NSSearchPathDirectory.DocumentDirectory) 

The URLs will be displayed on the right side of the playground, and then just copy and paste in the dialog box Finder → Go → Go to Folder.

GitHub Repo: For a complete working example, see this GitHub repo .

+2
source

Source: https://habr.com/ru/post/1214813/


All Articles