I developed an iPhone game in Swift using Xcode. It needs to store settings as well as user data. How do I get the app's settings and data to save when it is restarted or quit?
-
1Save your settings in NSUserDefaults when the app closes and read them back when the app reopens. There's already plenty of examples on SO that you can follow. – Eric Aya Jan 13 '16 at 17:32
-
@EricD. D I know the NSUserDefaults before I asked here. but I heard it is not safe to a large file. I think I will be blocked from this question. thanks for the comment. – Kyu Jan 13 '16 at 17:38
-
You're right that NSUserDefaults is not suited for large files, but you should have told us in your question that you wanted to save large files and that you already knew about NSUserDefaults... How are we supposed to guess otherwise? :) – Eric Aya Jan 13 '16 at 17:39
-
@EricD. Yes, I missed that point. that is my mistake. ;-0 – Kyu Jan 13 '16 at 17:42
-
Why are "settings" a large file? Don't conflate "settings" and "user data". – Joshua Nozzi Jan 13 '16 at 17:44
-
@JoshuaNozzi My application contained a lot of image files and user can edit that image files. I think that point is not Good at to setting NSUserDefaults. – Kyu Jan 13 '16 at 17:52
-
I think there is now enough information to undo downvotes and retract votes to close. – Joshua Nozzi Jan 13 '16 at 17:56
-
@JoshuaNozzi thanks very much:-> – Kyu Jan 13 '16 at 18:01
1 Answers
As mentioned in comments, use NSUserDefaults
for settings. For "large files" it's more likely you mean "user data". In that case, you can use Core Data or store your own NSCoding
-compliant data model via NSKeyedArchiver
(and unarchived via NSKeyedUnarchiver
) or their ...Secure...
alternatives to write to your app's documents.
Rather than save only when the application's state changes, you should probably persist small changes as they're made (this is basically what NSUserDefaults
does) or as some logical group of changes are made (what constitutes a "logical group" depends entirely on the nature of the data and your app and is therefore up to you).
So: Identify the "settings" and store them the right way (in NSUserDefaults
). Then identify your user's game data and store that the right way (in some sort of data file).

- 60,946
- 14
- 140
- 135
-
For user data, see also, Caleb's answer to this SO Q&A: http://stackoverflow.com/questions/23740392/how-to-store-user-data-ios – Joshua Nozzi Jan 13 '16 at 17:54
-
-
Sure thing. :-) Adding: If you use Core Data, search the guide I linked for "external file". You could have an entity "Image" with metadata (like "title" and "width" / "height") stored in its properties but its actual binary file content stored as an external file. This will keep your Core Data store responsive and your memory footprint low when fetching/listing all images available to the user (ie, it avoids loading *everything*, contents and all, when all you want is metadata for a list). – Joshua Nozzi Jan 13 '16 at 18:02
-
If you go the non-Core-Data route (ie, your own distinct document formats), you'll probably want one file for your "manifest" (your list of images and their metadata) and a subfolder full of the actual images, which accomplishes the same thing as the Core Data comment I just posted above. – Joshua Nozzi Jan 13 '16 at 18:04