0
0
Swiftprogramming~10 mins

@propertyWrapper declaration 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([1]: Int) {
        self.wrappedValue = wrappedValue
    }
}
Drag options to blanks, or click blank then click option'
AdefaultValue
Bvalue
CinitialValue
DwrappedValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'initialValue' or 'value' as the init parameter label causes compilation errors.
2fill in blank
medium

Complete 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'
Avalue
Bproperty
CwrappedValue
Dcontent
Attempts:
3 left
💡 Hint
Common Mistakes
Using other property names like 'value' or 'content' will not work.
3fill in blank
hard

Fix 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'
Avalue
BwrappedValue
Cproperty
Dcontent
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' or other names instead of 'wrappedValue' breaks the wrapper.
4fill in blank
hard

Fill 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'
AwrappedValue
Bvalue
CinitialValue
DdefaultValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property or parameter names breaks the wrapper.
5fill in blank
hard

Fill 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'
AwrappedValue
BprojectedValue
CinitialValue
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up property names or init parameter causes errors.