0
0
iOS Swiftmobile~20 mins

Preference keys in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Preference Key Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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
    }
  }
}
APreference value changed to 3\nTotal: 3
BPreference value changed to 0\nTotal: 0
CPreference value changed to 6\nTotal: 6
DNo output printed; total remains 0
Attempts:
2 left
💡 Hint
Remember that the reduce function sums all values from children views.
lifecycle
intermediate
1: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?
ABefore any child views set their preference values
BAfter all child views have set their preference values, before the parent reads them
COnly when the parent view appears on screen
DEvery time the parent view's body is recomputed
Attempts:
2 left
💡 Hint
Think about how preferences aggregate values from children.
📝 Syntax
advanced
2: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.
A
struct StringPreferenceKey: PreferenceKey {
  static var defaultValue = ""
  static func reduce(value: String, nextValue: () -> String) {
    value += nextValue()
  }
}
B
struct StringPreferenceKey: PreferenceKey {
  static var defaultValue: String? = nil
  static func reduce(value: inout String, nextValue: () -> String) {
    value = nextValue()
  }
}
C
struct StringPreferenceKey: PreferenceKey {
  static var defaultValue: String = ""
  static func reduce(value: inout String, nextValue: () -> String) {
    value += nextValue()
  }
}
D
struct StringPreferenceKey: PreferenceKey {
  static var defaultValue: String = ""
  func reduce(value: inout String, nextValue: () -> String) {
    value += nextValue()
  }
}
Attempts:
2 left
💡 Hint
Check the method signature and static properties required by PreferenceKey protocol.
🔧 Debug
advanced
2: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) } }
AThe onPreferenceChange modifier is attached to ChildView, so it never receives the preference from ChildView itself.
BThe preference key's defaultValue is zero, so the preference never changes from default.
CThe ParentView's body does not include the Text displaying number inside the VStack or container, so it won't update properly.
DThe reduce function overwrites value instead of combining, so multiple children values are lost, but here only one child exists, so this is not the issue.
Attempts:
2 left
💡 Hint
Consider where onPreferenceChange is applied in the view hierarchy.
🧠 Conceptual
expert
1:30remaining
What is the main purpose of PreferenceKey in SwiftUI?
Which statement best describes the main use of PreferenceKey in SwiftUI?
ATo define custom animations for view transitions
BTo store user preferences persistently across app launches
CTo manage state locally within a single view without affecting others
DTo allow child views to communicate values up the view hierarchy to parent views in a decoupled way
Attempts:
2 left
💡 Hint
Think about data flow direction and how PreferenceKey helps views communicate.