Recall & Review
beginner
What is a UserDefaults key in iOS Swift development?
A UserDefaults key is a unique identifier used to store and retrieve user settings or preferences in UserDefaults. It helps keep data organized and accessible.
Click to reveal answer
beginner
How do you define a UserDefaults key for a Boolean value in Swift?
You create a constant string key, for example: <code>let darkModeKey = "darkModeEnabled"</code>, then use it to save or read a Boolean from UserDefaults.Click to reveal answer
beginner
Why should you use constants for UserDefaults keys instead of hardcoding strings everywhere?
Using constants avoids typos, makes code easier to maintain, and ensures consistency when accessing stored preferences.
Click to reveal answer
beginner
How do you save a value using a UserDefaults key in UserDefaults?
Use
UserDefaults.standard.set(value, forKey: key), where key is your UserDefaults key.Click to reveal answer
beginner
How do you read a value using a UserDefaults key from UserDefaults?
Use
UserDefaults.standard.bool(forKey: key) for Booleans or UserDefaults.standard.string(forKey: key) for strings, replacing key with your UserDefaults key.Click to reveal answer
What is the main purpose of a UserDefaults key in iOS development?
✗ Incorrect
UserDefaults keys uniquely identify user settings stored in UserDefaults.
Which class is commonly used to save and retrieve preferences using keys?
✗ Incorrect
UserDefaults is the class used to store and retrieve user preferences with keys.
Why is it better to use a constant for a UserDefaults key instead of a string literal?
✗ Incorrect
Constants prevent typos and help maintain consistent keys throughout the code.
How do you save a Boolean value to UserDefaults with a key?
✗ Incorrect
The correct method to save is set(_:forKey:).
Which method reads a Boolean value from UserDefaults?
✗ Incorrect
The method bool(forKey:) reads a Boolean value from UserDefaults.
Explain what a UserDefaults key is and how it is used in iOS Swift development.
Think about how apps remember user choices.
You got /4 concepts.
Describe the steps to save and read a Boolean preference using a UserDefaults key in Swift.
Focus on the methods used with UserDefaults.
You got /4 concepts.