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
Output of a property wrapper with initial configuration
What is the output of this Swift code using a property wrapper with configuration?
Swift
import Foundation @propertyWrapper struct Clamped { private var value: Int private let range: ClosedRange<Int> init(wrappedValue: Int, _ range: ClosedRange<Int>) { self.range = range self.value = min(max(wrappedValue, range.lowerBound), range.upperBound) } var wrappedValue: Int { get { value } set { value = min(max(newValue, range.lowerBound), range.upperBound) } } } struct Player { @Clamped(0...100) var health: Int = 150 } let player = Player() print(player.health)
Attempts:
2 left
💡 Hint
Think about how the initial value is adjusted to fit the allowed range.
✗ Incorrect
The property wrapper clamps the initial value 150 to the maximum of the range 0...100, so the stored value becomes 100.
❓ Predict Output
intermediate2:00remaining
Effect of changing wrappedValue in a configured property wrapper
What will be printed after modifying the wrappedValue in this Swift code?
Swift
import Foundation @propertyWrapper struct LimitedString { private var value: String private let maxLength: Int init(wrappedValue: String, maxLength: Int) { self.maxLength = maxLength self.value = String(wrappedValue.prefix(maxLength)) } var wrappedValue: String { get { value } set { value = String(newValue.prefix(maxLength)) } } } struct Message { @LimitedString(maxLength: 5) var text: String = "HelloWorld" } var msg = Message() msg.text = "SwiftProgramming" print(msg.text)
Attempts:
2 left
💡 Hint
Check how the setter limits the string length.
✗ Incorrect
The setter truncates the new string to the first 5 characters, so "SwiftProgramming" becomes "Swift".
🔧 Debug
advanced2:00remaining
Identify the error in this property wrapper with configuration
What error does this Swift code produce when compiled?
Swift
import Foundation @propertyWrapper struct Positive { private var value: Int init(wrappedValue: Int) { if wrappedValue > 0 { value = wrappedValue } } var wrappedValue: Int { get { value } set { value = newValue > 0 ? newValue : 1 } } } struct Account { @Positive var balance: Int = -10 } let acc = Account() print(acc.balance)
Attempts:
2 left
💡 Hint
Check if all paths in init assign 'value'.
✗ Incorrect
The init does not assign 'value' if wrappedValue <= 0, so 'value' is not initialized, causing a compile-time error.
❓ Predict Output
advanced2:00remaining
Output of a property wrapper with projectedValue configuration
What is the output of this Swift code using a property wrapper with projectedValue?
Swift
import Foundation @propertyWrapper struct Logged<Value> { private var value: Value var projectedValue: String = "" init(wrappedValue: Value) { self.value = wrappedValue self.projectedValue = "Initialized with \(wrappedValue)" } var wrappedValue: Value { get { value } set { value = newValue projectedValue = "Changed to \(newValue)" } } } struct Config { @Logged var setting: Int = 10 } var config = Config() config.setting = 20 print(config.$setting)
Attempts:
2 left
💡 Hint
Look at when projectedValue changes.
✗ Incorrect
The projectedValue updates when the wrappedValue changes, so after setting to 20, it prints "Changed to 20".
❓ Predict Output
expert3:00remaining
Final value after multiple property wrapper assignments with configuration
What is the final printed value of 'score' after all assignments in this Swift code?
Swift
import Foundation @propertyWrapper struct ScoreLimiter { private var value: Int private let minScore: Int private let maxScore: Int init(wrappedValue: Int, min: Int, max: Int) { self.minScore = min self.maxScore = max self.value = min(max(wrappedValue, min), max) } var wrappedValue: Int { get { value } set { value = min(max(newValue, minScore), maxScore) } } } struct Game { @ScoreLimiter(min: 0, max: 50) var score: Int = 60 } var game = Game() game.score = -10 game.score = 30 game.score = 100 print(game.score)
Attempts:
2 left
💡 Hint
Remember the property wrapper clamps values on assignment.
✗ Incorrect
Initial value 60 is clamped to 50. Then -10 clamps to 0, then 30 is valid, then 100 clamps to 50. Final value is 50.