Swift Filled plist is empty next time I open up the application

138 views Asked by At

When I'm opening up my application for the first time I have an empty plist that I fill with some values (writeToFile). I can read from the plist dynamically and get those values I just put into it. This far this good. Then I close my application and later on I open it again. At this point I want my plist to contain those values I dynamically wrote to it the previous time I opened the application, but to my disadvantage it's empty.

Is this a normal behaviour? Can I not use the plist as a "local database" where I save values dynamically, and read them some other time when the application is opened?

Thanks!

1

There are 1 answers

3
idmean On

Files in "supporting files" are not supposed to be modified.

The files of the supporting files group are (like all other non-code files) copied into the app bundle. Apple says about the app bundle:

This directory contains the app and all of its resources. You cannot write to this directory. [...] Writing to this directory changes the signature and prevents your app from launching. You can, however, gain read-only access to any resources stored in the apps bundle.

If you want to store users preferences, selection, etc. you should create the plist file in the documents directory.

Use this directory to store user-generated content. The contents of this directory can be made available to the user through file sharing; therefore, his directory should only contain files that you may wish to expose to the user. The contents of this directory are backed up by iTunes

You can get the path to the documents directory using:

NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last as! String

If you want to store preferences you may also want to take a look at NSUserDefaults.


https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html