Sobes.tech
Junior — Middle

How is data read from UserDefaults in iOS applications?

sobes.tech AI

Answer from AI

In iOS, the UserDefaults class is used for reading data from UserDefaults. It allows you to save and retrieve simple data types such as strings, numbers, boolean values, etc.

Example of reading a value:

let defaults = UserDefaults.standard

// Reading a string by key
if let username = defaults.string(forKey: "username") {
    print("Username: \(username)")
} else {
    print("Username not found")
}

// Reading a boolean value
let isLoggedIn = defaults.bool(forKey: "isLoggedIn")
print("User logged in: \(isLoggedIn)")

If the value for the key is missing, the methods return nil (for optional types) or a default value (e.g., false for bool). This is a simple and convenient way to store settings and small data between app launches.

How is data read from UserDefaults in iOS… - sobes.tech