Save Data to .plist File in Swift

Apparently the file is not in a writable location, so I created it in the documents directory. var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String var path = paths.stringByAppendingPathComponent(“data.plist”) var fileManager = NSFileManager.defaultManager() if (!(fileManager.fileExistsAtPath(path))) { var bundle : NSString = NSBundle.mainBundle().pathForResource(“data”, ofType: “plist”) fileManager.copyItemAtPath(bundle, toPath: path, error:nil) } data.setObject(object, forKey: “object”) data.writeToFile(path, atomically: true) … Read more

How to find NSDocumentDirectory in Swift?

Apparently, the compiler thinks NSSearchPathDirectory:0 is an array, and of course it expects the type NSSearchPathDirectory instead. Certainly not a helpful error message. But as to the reasons: First, you are confusing the argument names and types. Take a look at the function definition: func NSSearchPathForDirectoriesInDomains( directory: NSSearchPathDirectory, domainMask: NSSearchPathDomainMask, expandTilde: Bool) -> AnyObject[]! directory … Read more

What is the documents directory (NSDocumentDirectory)?

Your app only (on a non-jailbroken device) runs in a “sandboxed” environment. This means that it can only access files and directories within its own contents. For example Documents and Library. See the iOS Application Programming Guide. To access the Documents directory of your applications sandbox, you can use the following: iOS 8 and newer, … Read more