Challenge - 5 Problems
Preference Key Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this SwiftUI PreferenceKey example?
Consider this SwiftUI code that uses a custom PreferenceKey to pass a value up the view tree. What will be printed in the console when the button is tapped?
iOS Swift
struct MyPreferenceKey: PreferenceKey {
static var defaultValue: Int = 0
static func reduce(value: inout Int, nextValue: () -> Int) {
value += nextValue()
}
}
struct ContentView: View {
@State private var total = 0
var body: some View {
VStack {
Text("Total: \(total)")
Button("Tap me") {}
.preference(key: MyPreferenceKey.self, value: 3)
}
.onPreferenceChange(MyPreferenceKey.self) { value in
print("Preference value changed to \(value)")
total = value
}
}
}Attempts:
2 left
💡 Hint
Remember that the reduce function sums all values from children views.
✗ Incorrect
The button sets a preference value of 3. The reduce function adds values, but since only one value is set, the total is 3. The onPreferenceChange updates the state and prints the new value.
❓ lifecycle
intermediate1:30remaining
When is the reduce function of a PreferenceKey called?
In SwiftUI, the reduce(value:nextValue:) method of a PreferenceKey is called at which point during the view update cycle?
Attempts:
2 left
💡 Hint
Think about how preferences aggregate values from children.
✗ Incorrect
The reduce function combines all preference values from child views after they have been set, so the parent can read the combined result.
📝 Syntax
advanced2:30remaining
Which option correctly defines a PreferenceKey with a String default value?
Select the correct Swift code that defines a PreferenceKey with a default value of an empty string and concatenates child values.
Attempts:
2 left
💡 Hint
Check the method signature and static properties required by PreferenceKey protocol.
✗ Incorrect
Option C correctly declares defaultValue as static var with type String and implements static reduce with inout parameter. Others have wrong signatures or missing static.
🔧 Debug
advanced2:30remaining
Why does this PreferenceKey not update the parent view?
Given this code, why does the parent view not update when the child sets a preference value?
struct MyKey: PreferenceKey {
static var defaultValue: Int = 0
static func reduce(value: inout Int, nextValue: () -> Int) {
value = nextValue()
}
}
struct ParentView: View {
@State private var number = 0
var body: some View {
ChildView()
.onPreferenceChange(MyKey.self) { value in
number = value
}
Text("Number: \(number)")
}
}
struct ChildView: View {
var body: some View {
Text("Child")
.preference(key: MyKey.self, value: 5)
}
}
Attempts:
2 left
💡 Hint
Consider where onPreferenceChange is applied in the view hierarchy.
✗ Incorrect
onPreferenceChange must be attached to a parent view that contains the child setting the preference. Here, attaching it directly to ChildView means it won't observe changes from ChildView itself.
🧠 Conceptual
expert1:30remaining
What is the main purpose of PreferenceKey in SwiftUI?
Which statement best describes the main use of PreferenceKey in SwiftUI?
Attempts:
2 left
💡 Hint
Think about data flow direction and how PreferenceKey helps views communicate.
✗ Incorrect
PreferenceKey is designed to let child views send data up to their ancestors without direct bindings, enabling flexible UI designs.