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 property wrapper example?
Consider this Swift code using a property wrapper. What will be printed when accessing
user.name and user.$name?Swift
import Foundation @propertyWrapper struct Capitalized { private var value: String = "" var wrappedValue: String { get { value } set { value = newValue.capitalized } } var projectedValue: String { "Projected: \(value)" } } struct User { @Capitalized var name: String } var user = User() user.name = "john doe" print(user.name) print(user.$name)
Attempts:
2 left
💡 Hint
Remember that wrappedValue applies capitalization, and projectedValue returns a string with prefix.
✗ Incorrect
The wrappedValue setter capitalizes the string, so user.name prints "John Doe". The projectedValue returns "Projected: John Doe".
🧠 Conceptual
intermediate1:30remaining
What does the projectedValue property provide in a Swift property wrapper?
In Swift property wrappers, what is the main purpose of the
projectedValue property?Attempts:
2 left
💡 Hint
Think about how you access the property with a $ prefix.
✗ Incorrect
The projectedValue allows access to extra features or metadata related to the wrapped property, accessed via the $ prefix.
🔧 Debug
advanced2:30remaining
Why does this Swift code cause a compile error?
Examine this property wrapper code. Why does it cause a compile error?
@propertyWrapper
struct Wrapper {
var wrappedValue: Int
var projectedValue: Int {
wrappedValue * 2
}
}
struct Example {
@Wrapper var number: Int
}
let e = Example(number: 5)
print(e.$number)
Swift
import Foundation @propertyWrapper struct Wrapper { var wrappedValue: Int var projectedValue: Int { wrappedValue * 2 } } struct Example { @Wrapper var number: Int } let e = Example(number: 5) print(e.$number)
Attempts:
2 left
💡 Hint
Property wrappers need an initializer if wrappedValue is not optional or has no default.
✗ Incorrect
The compiler requires an init(wrappedValue:) initializer to set the wrappedValue when used with property wrappers. Without it, the code fails.
📝 Syntax
advanced1:30remaining
Which option correctly defines a projectedValue in a Swift property wrapper?
Choose the correct syntax for defining a projectedValue property in a Swift property wrapper that returns a String.
Attempts:
2 left
💡 Hint
projectedValue is a computed property, not a method or constant.
✗ Incorrect
The projectedValue must be a computed property with a getter, not a function or constant.
🚀 Application
expert3:00remaining
How many items are in the dictionary after this Swift code runs?
Given this Swift property wrapper and usage, how many key-value pairs are in the dictionary
counts after execution?Swift
@propertyWrapper struct Counted { private static var counts = [String: Int]() private let key: String var wrappedValue: String { didSet { Self.counts[key, default: 0] += 1 } } var projectedValue: Int { Self.counts[key] ?? 0 } init(wrappedValue: String, key: String) { self.wrappedValue = wrappedValue self.key = key Self.counts[key] = 0 } } struct Test { @Counted(key: "a") var first = "Hello" @Counted(key: "b") var second = "World" } var t = Test() t.first = "Hi" t.second = "Earth" t.first = "Hey"
Attempts:
2 left
💡 Hint
Each key is unique and increments count on each wrappedValue change.
✗ Incorrect
There are two keys "a" and "b" in the static dictionary. Each key is initialized once, so the dictionary has 2 items.