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([1]: Int) {
self.wrappedValue = wrappedValue
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'initialValue' or 'value' as the init parameter label causes compilation errors.
✗ Incorrect
The initializer parameter label for a property wrapper must be 'wrappedValue' to match the synthesized initializer call from the compiler.
2fill in blank
mediumComplete the code to declare a property wrapper with a wrappedValue of type String.
Swift
@propertyWrapper
struct Wrapper {
var [1]: String
init(wrappedValue: String) {
self.wrappedValue = wrappedValue
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using other property names like 'value' or 'content' will not work.
✗ Incorrect
The property wrapper must have a 'wrappedValue' property to work correctly.
3fill in blank
hardFix the error in the property wrapper declaration by completing the code.
Swift
@propertyWrapper
struct Wrapper {
private var value: Int
var [1]: Int {
get { value }
set { value = newValue }
}
init(wrappedValue: Int) {
self.value = wrappedValue
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' or other names instead of 'wrappedValue' breaks the wrapper.
✗ Incorrect
The computed property exposing the wrapped value must be named 'wrappedValue' for the property wrapper to work.
4fill in blank
hardFill both blanks to complete a property wrapper that clamps an Int value between 0 and 100.
Swift
@propertyWrapper
struct Clamped {
private var value: Int
var [1]: Int {
get { value }
set { value = min(max(newValue, 0), 100) }
}
init([2]: Int) {
self.value = min(max([2], 0), 100)
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property or parameter names breaks the wrapper.
✗ Incorrect
The property wrapper must use 'wrappedValue' for the exposed property and 'wrappedValue' as the init parameter label.
5fill in blank
hardFill all three blanks to complete a property wrapper that stores a String and provides a projectedValue as uppercase.
Swift
@propertyWrapper
struct Uppercase {
private var value: String
var [1]: String {
get { value }
set { value = newValue }
}
var [2]: String {
value.uppercased()
}
init([3]: String) {
self.value = [3]
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up property names or init parameter causes errors.
✗ Incorrect
The property wrapper uses 'wrappedValue' for the main value, 'projectedValue' for the extra value, and 'wrappedValue' as the init parameter label.