Challenge - 5 Problems
Swift Property Wrapper Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Swift code using @propertyWrapper?
Consider the following Swift code that uses a property wrapper. What will be printed when running this code?
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
Look at how the setter modifies the string using the capitalized property.
✗ Incorrect
The @propertyWrapper Capitalized modifies the string when it is set by capitalizing it. So when "john doe" is assigned, it becomes "John Doe".
❓ Predict Output
intermediate2:00remaining
What error does this Swift @propertyWrapper code produce?
Examine this Swift code using @propertyWrapper. What error will it produce when compiled?
Swift
@propertyWrapper struct NumberWrapper { var wrappedValue: Int init() { wrappedValue = 0 } } struct Data { @NumberWrapper var number: Int } let d = Data()
Attempts:
2 left
💡 Hint
Check how the property wrapper is initialized and what initializers are available.
✗ Incorrect
The property wrapper NumberWrapper has an init() without parameters, but the compiler expects an init(wrappedValue:) initializer to initialize the wrapped property. Since it is missing, the compiler complains about missing argument for wrappedValue.
🔧 Debug
advanced2:00remaining
Why does this @propertyWrapper code cause a runtime crash?
This Swift code compiles but crashes at runtime. What is the cause of the crash?
Swift
@propertyWrapper struct Positive { private var value: Int var wrappedValue: Int { get { value } set { guard newValue > 0 else { fatalError("Value must be positive") } value = newValue } } init(wrappedValue: Int) { self.wrappedValue = wrappedValue } } struct Account { @Positive var balance: Int } let a = Account(balance: -10)
Attempts:
2 left
💡 Hint
Look at how the initial wrappedValue is assigned and how the setter guard works.
✗ Incorrect
The Account(balance: -10) calls Positive.init(wrappedValue: -10), which assigns self.wrappedValue = -10. This invokes the setter of wrappedValue. The guard in the setter checks if newValue > 0, which fails, triggering fatalError("Value must be positive").
📝 Syntax
advanced2:00remaining
Which option correctly declares a @propertyWrapper with projectedValue?
Choose the correct Swift code that declares a @propertyWrapper with a projectedValue property.
Attempts:
2 left
💡 Hint
The projectedValue must be a readable property, usually with a getter.
✗ Incorrect
Option C correctly declares projectedValue with a getter that returns wrappedValue * 2. Option C returns a String but does not specify a getter explicitly (which is allowed but less common). Option C uses valid shorthand syntax for a read-only computed property. Option C declares only a setter without a getter, which is invalid.
🚀 Application
expert2:00remaining
How many items are in the resulting array after using this @propertyWrapper in a loop?
Given this Swift code using a @propertyWrapper that tracks assignments, how many elements will be in the assignments array after the loop?
Swift
@propertyWrapper class Tracker { private(set) var assignments: [Int] = [] var wrappedValue: Int { didSet { assignments.append(wrappedValue) } } init(wrappedValue: Int) { self.wrappedValue = wrappedValue } } struct Data { @Tracker var value: Int } var data = Data(value: 0) for i in 1...3 { data.value = i } let count = data.$value.assignments.count print(count)
Attempts:
2 left
💡 Hint
Remember the initial assignment in init and the didSet calls in the loop.
✗ Incorrect
The assignments array starts with the initial wrappedValue 0 added in init (via didSet). Then each assignment in the loop (1, 2, 3) triggers didSet appending the new value. So total 4 elements are appended.