Saving Data in a Swift App
People new to iOS development frequently ask how to save data in their apps. Apple provides the following technologies for saving app data on a local device:
- UserDefaults
- Keychain
- Files
- Core Data
You can use multiple methods to save data in your app. Many iOS and Mac apps use both UserDefaults and Core Data to save data. Use all the methods for saving data that make sense for your app.
UserDefaults
Apple provides a UserDefaults
class that provides a programming interface to your app’s defaults database. The defaults database contains user settings and preferences.
Use UserDefaults to store small amounts of data, like settings and preferences. If you are writing a plain text editor, you could store the font to use in UserDefaults.
Keychain
Use the keychain to store small amounts of data securely, such as passwords. If you were writing a Twitter app, you could use the keychain to store the person’s Twitter handle and password.
Apple provides a Keychain Services API for apps to work with the keychain. The documentation is in the System > Security section of Apple’s documentation in Xcode. Choose Help > Developer Documentation in Xcode to read the documentation.
The following article provides an introduction on working with the keychain:
Saving Passwords in the Keychain in Swift
Files
You can use files to store larger amounts of data. Document-based apps, such as text editors, spreadsheets, and drawing apps, usually save their data in files.
Apple provides the Codable
protocol to simplify reading and writing data to files. An Internet search for Swift Codable tutorial yields many results, most of which cover JSON.
Core Data
Core Data is Apple’s framework for creating and working with a data model for iOS and Mac apps. One of the things Core Data does is save data for you.
Core Data is a huge subject. People have written books about it. You can find an overview of Core Data in Apple’s documentation in Xcode under App Services. Apple also has a Core Data Programming Guide on their developer website.
Core Data or Files?
Should you use Core Data or files to save larger amounts of data?
Core Data is a powerful framework that does a lot, much more than saving app data. But with that power comes a learning curve.
You must determine if your app’s data needs require Core Data. Avoid Core Data (or at least do some more research before using Core Data) in the following cases:
- Your app supports non-Apple platforms like Android, Linux, and Windows. Core Data does not run on non-Apple platforms.
- Your data can easily be saved in one file.
- Your app data has only one class or struct.
- You are writing a document-based app. Core Data supports document-based apps, but it doesn’t add much to Apple’s document architecture.
A text editor is an example of a project where you should save your data in a file. The app data is just a bunch of text. The data can easily be saved in one file. Core Data would not help you much.
Use Core Data to save your app’s data in the following cases:
- You store large amounts of data in the app.
- The data makes more sense to store in a database than in a single file.
- Your app data has multiple classes or structs.
- You are writing a shoebox (not document-based) app.
A blog editor is an example of a project where you should use Core Data. A blog can contain hundreds of posts that would be difficult to store in a file. A blog has multiple pieces of data: titles, tags, categories, and authors. Core Data can do things like show you all the blog posts with a given tag. Take advantage of Core Data if it will help for your app.
If you are still not sure whether or not to use Core Data, use it.