Challenge - 5 Problems
Swift Property Wrapper Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple property wrapper usage
What is the output of this Swift code using a property wrapper?
Swift
import Foundation @propertyWrapper struct Capitalized { private var value: String = "" var wrappedValue: String { get { value } set { value = newValue.capitalized } } } struct Person { @Capitalized var name: String } var p = Person() p.name = "john doe" print(p.name)
Attempts:
2 left
💡 Hint
Think about what the property wrapper does when setting the value.
✗ Incorrect
The property wrapper Capitalized changes the string to capitalized form when setting it. So "john doe" becomes "John Doe".
🧠 Conceptual
intermediate1:30remaining
Why do property wrappers reduce boilerplate?
Which of the following best explains why property wrappers reduce boilerplate code in Swift?
Attempts:
2 left
💡 Hint
Think about how property wrappers help avoid repeating code.
✗ Incorrect
Property wrappers let you define common behavior for property access in one place, so you don't repeat the same getter/setter code for every property.
🔧 Debug
advanced2:30remaining
Identify the error in this property wrapper usage
What error will this Swift code produce?
Swift
@propertyWrapper struct NonEmpty { var wrappedValue: String { didSet { if wrappedValue.isEmpty { wrappedValue = "Default" } } } init(wrappedValue: String) { self.wrappedValue = wrappedValue if wrappedValue.isEmpty { self.wrappedValue = "Default" } } } struct User { @NonEmpty var username: String } let u = User(username: "")
Attempts:
2 left
💡 Hint
Check what happens when wrappedValue is set inside didSet.
✗ Incorrect
Setting wrappedValue inside didSet triggers didSet again, causing infinite recursion and a runtime crash.
❓ Predict Output
advanced2:30remaining
Output of property wrapper with projected value
What will be printed by this Swift code?
Swift
@propertyWrapper struct Logged<Value> { private var value: Value var wrappedValue: Value { get { value } set { print("Setting value to \(newValue)") value = newValue } } var projectedValue: String { "Value is currently \(value)" } init(wrappedValue: Value) { self.value = wrappedValue } } struct Config { @Logged var setting: Int = 10 } var c = Config() print(c.$setting) c.setting = 20
Attempts:
2 left
💡 Hint
Remember when the setter print runs and what $property means.
✗ Incorrect
The initial value is set in init without calling setter, so no print then. Printing c.$setting shows projectedValue. Setting c.setting to 20 triggers print in setter.
🧠 Conceptual
expert2:00remaining
How do property wrappers improve code maintainability?
Which statement best describes how property wrappers improve maintainability in Swift code?
Attempts:
2 left
💡 Hint
Think about how changing one property wrapper affects all properties using it.
✗ Incorrect
By putting shared logic in a property wrapper, you only need to change that wrapper to update behavior everywhere it is used, improving maintainability.