Swift version: 5.1
All iOS apps have a built in data dictionary that stores small amounts of user settings for as long as the app is installed. This system, called
UserDefaults can save integers, booleans, strings, arrays, dictionaries, dates and more, but you should be careful not to save too much data because it will slow the launch of your app.
Here's an example of setting some values:
let defaults = UserDefaults.standard defaults.set(25, forKey: "Age") defaults.set(true, forKey: "UseTouchID") defaults.set(CGFloat.pi, forKey: "Pi") defaults.set("Paul Hudson", forKey: "Name") defaults.set(Date(), forKey: "LastRun")
When you set values like that, they become permanent – you can quit the app then re-launch and they'll still be there, so it's the ideal way to store app configuration data. As an advance warning, you might find some old tutorials recommend calling the
synchronize() method to force your data to save, but Apple has asked us not to do that for some years now.
As mentioned, you can use
UserDefaults to store arrays and dictionaries, like this:
let array = ["Hello", "World"] defaults.set(array, forKey: "SavedArray") let dict = ["Name": "Paul", "Country": "UK"] defaults.set(dict, forKey: "SavedDict")
When it comes to reading data back, it's still easy but has an important proviso:
UserDefaults will return a default value if the setting can't be found. You need to know what these default values are so that you don't confuse them with real values that you set. Here they are:
integer(forKey:)returns an integer if the key existed, or 0 if not.
bool(forKey:)returns a boolean if the key existed, or false if not.
float(forKey:)returns a float if the key existed, or 0.0 if not.
double(forKey:)returns a double if the key existed, or 0.0 if not.
AnyObject?so you need to conditionally typecast it to your data type.
With that in mind, you can read values back like this:
let age = defaults.integer(forKey: "Age") let useTouchID = defaults.bool(forKey: "UseTouchID") let pi = defaults.double(forKey: "Pi")
When retrieving objects, the result is optional. This means you can either accept the optionality, or typecast it to a non-optional type and use the nil coalescing operator to handle missing values. For example:
let savedArray = defaults.object(forKey: "SavedArray") as? [String] ?? [String]()
SPONSORED Catch bugs as soon as they happen and know exactly why a crash occurred by integrating Instabug's SDK in one minute. You will automatically receive device data, network logs, and reproduction steps with every bug and crash report.
Available from iOS 2.0 – see Hacking with Swift tutorial 12
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.