How to Save and Retrieve Data with UserDefaults (Swift)
This article explains how to save and retrieve data using UserDefaults in Swift.
What Is UserDefaults?
There are several ways to persist data on a device from within an iOS app.
UserDefaults is one of those options, letting you easily store and fetch data on the device as key–value pairs.
It's best suited for small pieces of data—like app settings—and can be accessed from anywhere in your app.
If you need to store larger amounts of data on the device, consider solutions like Core Data or SQLite instead.
How to Save and Retrieve Data with UserDefaults
To save data to UserDefaults in Swift, use the set() methods.
func set(Any?, forKey: String)
func set(Float, forKey: String)
func set(Double, forKey: String)
func set(Int, forKey: String)
func set(Bool, forKey: String)
func set(URL?, forKey: String)
To retrieve data from UserDefaults, use the method that matches the type you stored.
func object(forKey: String) -> Any?
func url(forKey: String) -> URL?
func array(forKey: String) -> [Any]?
func dictionary(forKey: String) -> [String : Any]?
func string(forKey: String) -> String?
func stringArray(forKey: String) -> [String]?
func data(forKey: String) -> Data?
func bool(forKey: String) -> Bool
func integer(forKey: String) -> Int
func float(forKey: String) -> Float
func double(forKey: String) -> Double
func dictionaryRepresentation() -> [String : Any]
For example, to save and retrieve a Bool under the key "UseLocation":
// Save
UserDefaults.standard.set(true, forKey: "UseLocation")
// Retrieve
let useLocation = UserDefaults.standard.bool(forKey: "UseLocation")
print(useLocation) // true
To save a Dictionary under the key "UserInfo":
// Save
let dict:[String : Any] = ["Name": "Smith", "Age": 15, "Gender": "M"]
UserDefaults.standard.set(dict, forKey: "UserInfo")
// Retrieve
if let userInfo = UserDefaults.standard.dictionary(forKey: "UserInfo") {
print(userInfo) // ["Gender": M, "Age": 15, "Name": Smith]
}
If you want to store an instance of your own struct or class, make it conform to Codable, then encode/decode it to/from JSON Data when saving and retrieving.
For example, to save and fetch an instance of the following User struct under the key "UserData":
struct User: Codable {
var UserID: Int
var UserName: String
var Age: Int
}
// Save
let user = User(UserID: 1, UserName: "Smith", Age: 15)
if let jsonData = try? JSONEncoder().encode(user) {
UserDefaults.standard.set(jsonData, forKey: "UserData")
}
// Retrieve
if let data = UserDefaults.standard.data(forKey: "UserData") {
if let userData = try? JSONDecoder().decode(User.self, from: data) {
print(userData) // User(UserID: 1, UserName: "Smith", Age: 15)
}
}
How to Remove Data from UserDefaults
To delete data from UserDefaults in Swift, use removeObject().
func removeObject(forKey: String)
For example, to remove the "UseLocation" key–value pair we added earlier:
UserDefaults.standard.removeObject(forKey: "UseLocation")
That's it—now you know how to save, retrieve, and remove data with UserDefaults in Swift.