-
Re: Storing data
karim Aug 16, 2017 5:43 PM (in response to Adalex3)I typically would either:
- serialize the data as JSON or a PLIST or other structured file format and save into the application data directory
- create a SQLlite database on first app startup in the data directory, and use that for persistence
- use CoreData (not a novice technology)
-
Re: Storing data
Claude31 Aug 17, 2017 12:29 AM (in response to karim)The simplest is to save in NSUserDefaults
When you have changed a value:
let defaults = NSUserDefaults.standardUserDefaults() defaults.setObject(valueToSave, forKey: keyForTheValue) defaults.synchronize()
-
Re: Storing data
eskimo Aug 17, 2017 3:12 AM (in response to Claude31)The simplest is to save in NSUserDefaults
I recommend against that. First of all, Swift programmers should use the Swift-native
UserDefaults
rather than NSUserDefaults. Beyond that, however, the user defaults mechanism was designed for storing preferences, not the main data of your app. Before using user defaults to store some data you should ask yourself “How would the user react if this data was lost?” If the reaction is “sigh I have to go and change a preference”, go ahead and use user defaults. If the reaction involves pitchforks and flaming torches, do the work to manage this data yourself (karim gave an excellent list of options for this).Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardwarelet myEmail = "eskimo" + "1" + "@apple.com"
-