0
0
Swiftprogramming~10 mins

Why property wrappers reduce boilerplate in Swift - Test Your Understanding

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 Capitalized.

Swift
propertyWrapper struct Capitalized {
    private var value: String = ""
    var wrappedValue: String {
        get { value }
        set { value = newValue[1] }
    }
}
Drag options to blanks, or click blank then click option'
A.lowercased()
B.capitalized
C.uppercased()
D.trimmingCharacters(in: .whitespaces)
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method that changes case incorrectly, like lowercased or uppercased.
Forgetting to apply the method to the newValue.
2fill in blank
medium

Complete the code to apply the Capitalized property wrapper to the name property.

Swift
struct Person {
    @[1] var name: String
}
Drag options to blanks, or click blank then click option'
ALowercased
BTrimmed
CCapitalized
DUppercased
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong wrapper name or forgetting the '@' symbol.
Confusing capitalization with other string transformations.
3fill in blank
hard

Fix the error in the property wrapper by completing the missing initializer.

Swift
propertyWrapper struct Capitalized {
    private var value: String = ""
    var wrappedValue: String {
        get { value }
        set { value = newValue.capitalized }
    }
    init([1]) {
        self.wrappedValue = wrappedValue
    }
}
Drag options to blanks, or click blank then click option'
AwrappedValue: String
Bvalue: String
CnewValue: String
DinitialValue: String
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name other than wrappedValue.
Not initializing the property wrapper's value properly.
4fill in blank
hard

Fill both blanks to create a property wrapper that trims whitespace and capitalizes the string.

Swift
propertyWrapper struct TrimmedCapitalized {
    private var value: String = ""
    var wrappedValue: String {
        get { value }
        set { value = newValue[1][2] }
    }
}
Drag options to blanks, or click blank then click option'
A.trimmingCharacters(in: .whitespaces)
B.capitalized
C.lowercased()
D.uppercased()
Attempts:
3 left
💡 Hint
Common Mistakes
Reversing the order of trimming and capitalization.
Using incorrect string methods.
5fill in blank
hard

Fill all three blanks to create a property wrapper that trims, capitalizes, and limits the string length to 10 characters.

Swift
propertyWrapper struct TrimCapLimit {
    private var value: String = ""
    var wrappedValue: String {
        get { value }
        set {
            let trimmed = newValue[1]
            let capitalized = trimmed[2]
            value = String(capitalized.prefix([3]))
        }
    }
}
Drag options to blanks, or click blank then click option'
A.trimmingCharacters(in: .whitespaces)
B.capitalized
C10
D.lowercased()
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong order of string methods.
Forgetting to convert prefix result back to String.
Using an incorrect limit number.