Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a property wrapper named Wrapper.
Swift
@propertyWrapper
struct Wrapper {
var wrappedValue: Int
init(wrappedValue: Int) {
self.[1] = wrappedValue
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different property name than wrappedValue.
Forgetting to assign the wrappedValue in the initializer.
✗ Incorrect
The property wrapper must store the value in a property named wrappedValue.
2fill in blank
mediumComplete the code to add a configuration parameter to the property wrapper.
Swift
@propertyWrapper
struct Wrapper {
var wrappedValue: Int
var multiplier: Int
init(wrappedValue: Int, [1]: Int) {
self.multiplier = multiplier
self.wrappedValue = wrappedValue * multiplier
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the property name.
Not passing the configuration parameter in the initializer.
✗ Incorrect
The parameter name must match the property name multiplier to configure the wrapper.
3fill in blank
hardFix the error in the property wrapper usage with configuration.
Swift
struct Example {
@Wrapper([1]: 3) var number: Int = 5
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong argument label that does not match the property wrapper initializer.
Omitting the argument label.
✗ Incorrect
The property wrapper expects a parameter named multiplier, so use multiplier: 3.
4fill in blank
hardFill both blanks to complete the property wrapper that doubles the value if enabled.
Swift
@propertyWrapper
struct DoubleIfEnabled {
var wrappedValue: Int
var enabled: Bool
init(wrappedValue: Int, [1]: Bool) {
self.enabled = enabled
self.wrappedValue = enabled ? wrappedValue [2] 2 : wrappedValue
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong operator like + or - instead of *.
Using a different parameter name than enabled.
✗ Incorrect
The parameter is named enabled and the value is doubled using the * operator.
5fill in blank
hardFill all three blanks to create a property wrapper that clamps a value between min and max.
Swift
@propertyWrapper
struct Clamped {
var wrappedValue: Int
var min: Int
var max: Int
init(wrappedValue: Int, [1]: Int, [2]: Int) {
self.min = min
self.max = max
self.wrappedValue = max(min, min(wrappedValue, [3]))
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up min and max parameter names.
Using min instead of max in the clamping expression.
✗ Incorrect
The parameters are min and max; the wrappedValue is clamped using max(min, min(wrappedValue, max)).