0
0
Swiftprogramming~10 mins

Property wrappers with configuration in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AwrappedValue
Bvalue
Cproperty
Dwrapper
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different property name than wrappedValue.
Forgetting to assign the wrappedValue in the initializer.
2fill in blank
medium

Complete 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'
Aconfig
Bfactor
Cvalue
Dmultiplier
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the property name.
Not passing the configuration parameter in the initializer.
3fill in blank
hard

Fix 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'
Amultiplier
Bfactor
Cvalue
Dconfig
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong argument label that does not match the property wrapper initializer.
Omitting the argument label.
4fill in blank
hard

Fill 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'
Aenabled
B*
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong operator like + or - instead of *.
Using a different parameter name than enabled.
5fill in blank
hard

Fill 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'
Amin
Bmax
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up min and max parameter names.
Using min instead of max in the clamping expression.