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?
ATo handle animations
BTo create UI elements
CTo manage network requests
DTo uniquely identify stored user settings
✗ Incorrect
UserDefaults keys uniquely identify user settings stored in UserDefaults.
Which class is commonly used to save and retrieve preferences using keys?
AUserDefaults
BUIViewController
CNotificationCenter
DFileManager
✗ 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?
ATo make the app run faster
BTo avoid typos and keep code consistent
CTo reduce app size
DTo improve UI design
✗ Incorrect
Constants prevent typos and help maintain consistent keys throughout the code.
How do you save a Boolean value to UserDefaults with a key?
AUserDefaults.standard.save(true, "key")
BUserDefaults.standard.get(true, forKey: "key")
CUserDefaults.standard.set(true, forKey: "key")
DUserDefaults.standard.store(true, "key")
✗ Incorrect
The correct method to save is set(_:forKey:).
Which method reads a Boolean value from UserDefaults?
AUserDefaults.standard.bool(forKey: "key")
BUserDefaults.standard.getBool("key")
CUserDefaults.standard.readBool("key")
DUserDefaults.standard.fetchBool("key")
✗ 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.
Practice
(1/5)
1. What is the main purpose of PreferenceKey in SwiftUI?
easy
A. To store user preferences permanently
B. To share simple data up the view hierarchy
C. To create custom buttons
D. To manage network requests
Solution
Step 1: Understand PreferenceKey role
PreferenceKey is designed to pass data from child views up to parent views in SwiftUI.
Step 2: Differentiate from other uses
It is not for permanent storage or UI elements but for sharing layout or state info upwards.
Final Answer:
To share simple data up the view hierarchy -> Option B
Quick Check:
PreferenceKey = share data up [OK]
Hint: PreferenceKey passes data upward, not stores or creates UI [OK]
Common Mistakes:
Confusing PreferenceKey with UserDefaults
Thinking it creates UI components
Assuming it manages network tasks
2. Which of the following is the correct way to declare a PreferenceKey with a default value of 0 and a reduce function that sums values?
easy
A. struct MyKey: PreferenceKey { static var defaultValue = 0; static func reduce(value: Int, nextValue: () -> Int) { value += nextValue() } }
B. struct MyKey: PreferenceKey { static let defaultValue = 0; static func reduce(value: Int, nextValue: () -> Int) { value += nextValue() } }
C. struct MyKey: PreferenceKey { var defaultValue: Int = 0; func reduce(value: inout Int, nextValue: () -> Int) { value += nextValue() } }
D. struct MyKey: PreferenceKey { static var defaultValue: Int = 0; static func reduce(value: inout Int, nextValue: () -> Int) { value += nextValue() } }
Solution
Step 1: Check static defaultValue declaration
It must be a static property with explicit type: static var defaultValue: Int = 0.
Step 2: Verify reduce function signature
Reduce must be static, take inout value, and add nextValue: static func reduce(value: inout Int, nextValue: () -> Int).
Final Answer:
struct MyKey: PreferenceKey { static var defaultValue: Int = 0; static func reduce(value: inout Int, nextValue: () -> Int) { value += nextValue() } } -> Option D
Quick Check:
Static defaultValue and reduce with inout = struct MyKey: PreferenceKey { static var defaultValue: Int = 0; static func reduce(value: inout Int, nextValue: () -> Int) { value += nextValue() } } [OK]
Hint: defaultValue and reduce must be static with correct signatures [OK]
A. reduce function must have inout parameter for value
B. defaultValue must be non-static
C. nextValue must return Int, not CGSize
D. PreferenceKey cannot use CGSize type
Solution
Step 1: Check defaultValue declaration
defaultValue is correctly static with CGSize type.
Step 2: Inspect reduce function signature
Reduce must take inout for value to modify it, but here it is a value parameter.
Final Answer:
reduce function must have inout parameter for value -> Option A
Quick Check:
Reduce needs inout value parameter [OK]
Hint: Reduce must use inout for value to update it [OK]
Common Mistakes:
Forgetting inout keyword in reduce
Changing defaultValue to non-static
Wrong return type for nextValue
5. You want to pass the maximum width of child views up to a parent using PreferenceKey. Which implementation correctly combines widths to find the maximum?
hard
A. struct MaxWidthKey: PreferenceKey {
static var defaultValue: CGFloat = 0
static func reduce(value: CGFloat, nextValue: () -> CGFloat) {
value = max(value, nextValue())
}
}
B. struct MaxWidthKey: PreferenceKey {
static var defaultValue: CGFloat = 0
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value += nextValue()
}
}
C. struct MaxWidthKey: PreferenceKey {
static var defaultValue: CGFloat = 0
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value = max(value, nextValue())
}
}
D. struct MaxWidthKey: PreferenceKey {
static var defaultValue: CGFloat = 0
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value = min(value, nextValue())
}
}
Solution
Step 1: Identify correct reduce logic for max
To find maximum width, reduce must assign the max of current and next values.
Step 2: Verify function signature and logic
Reduce must be static, take inout value, and use value = max(value, nextValue()).
Final Answer:
struct MaxWidthKey: PreferenceKey { static var defaultValue: CGFloat = 0; static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) { value = max(value, nextValue()) } } -> Option C
Quick Check:
Use max in reduce with inout value [OK]
Hint: Use max function and inout value in reduce [OK]
Common Mistakes:
Using sum (+) instead of max
Missing inout in reduce parameter
Using min instead of max
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.