Recall & Review
beginner
What is UserDefaults used for in iOS development?
UserDefaults is used to store small pieces of data like settings or preferences that need to persist between app launches.
Click to reveal answer
beginner
How do you save a simple string value "Hello" for the key "greeting" using UserDefaults in Swift?
Use
UserDefaults.standard.set("Hello", forKey: "greeting") to save the string.Click to reveal answer
beginner
How do you retrieve a string value for the key "greeting" from UserDefaults safely?
Use
UserDefaults.standard.string(forKey: "greeting"). It returns an optional string, so you can safely unwrap it.Click to reveal answer
intermediate
Can UserDefaults store complex objects like custom classes directly?
No, UserDefaults is meant for simple data types like strings, numbers, booleans, and arrays. For complex objects, you need to encode them first.
Click to reveal answer
beginner
What happens if you try to read a value from UserDefaults for a key that does not exist?
You get nil or a default value depending on the method used. For example,
string(forKey:) returns nil if the key is missing.Click to reveal answer
Which method saves a boolean value to UserDefaults?
✗ Incorrect
The set(_:forKey:) method saves values. The bool(forKey:) method reads a boolean.
What type of data can UserDefaults store directly?
✗ Incorrect
UserDefaults supports simple data types like strings, numbers, booleans, arrays, and dictionaries.
How do you remove a value for a key from UserDefaults?
✗ Incorrect
removeObject(forKey:) deletes the value for the given key.
What does UserDefaults.standard.string(forKey: "name") return if "name" does not exist?
✗ Incorrect
It returns nil if the key does not exist.
Why should UserDefaults not be used for storing large data?
✗ Incorrect
UserDefaults is optimized for small pieces of data like settings, not large files.
Explain how to save and retrieve a user's preferred theme (dark or light) using UserDefaults in Swift.
Think about storing true or false for dark mode preference.
You got /4 concepts.
Describe the types of data suitable for UserDefaults and why it is not recommended for storing large files.
Consider what UserDefaults is designed for and what it is not.
You got /4 concepts.