0
0
Swiftprogramming~10 mins

Composing property wrappers 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 Capitalized that capitalizes a string.

Swift
@propertyWrapper struct Capitalized {
    private var value: String = ""
    var wrappedValue: String {
        get { value }
        set { value = newValue.capitalized }
    }
    init(wrappedValue: String) {
        self.value = wrappedValue.capitalized
    }
}
Drag options to blanks, or click blank then click option'
A.capitalized
B.uppercased()
C.lowercased()
D.trimmingCharacters(in: .whitespaces)
Attempts:
3 left
💡 Hint
Common Mistakes
Using .uppercased() which makes all letters uppercase instead of capitalizing each word.
Using .lowercased() which makes all letters lowercase.
Forgetting to capitalize the new value in the setter.
2fill in blank
medium

Complete the code to apply two property wrappers Trimmed and Capitalized to a string property name.

Swift
struct User {
    @[1] @Capitalized var name: String
}
Drag options to blanks, or click blank then click option'
ATrimmed
BLazy
CPublished
DState
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated wrappers like @Lazy or @State which do not trim strings.
Applying wrappers in the wrong order.
3fill in blank
hard

Fix the error in the composed property wrappers by completing the code to delegate projectedValue correctly.

Swift
@propertyWrapper struct Trimmed {
    private var value: String = ""
    var wrappedValue: String {
        get { value }
        set { value = newValue.trimmingCharacters(in: .whitespaces) }
    }
    var projectedValue: String { $[1] }
    init(wrappedValue: String) {
        self.value = wrappedValue.trimmingCharacters(in: .whitespaces)
    }
}
Drag options to blanks, or click blank then click option'
Avalue
BprojectedValue
Cself
DwrappedValue
Attempts:
3 left
💡 Hint
Common Mistakes
Returning value directly which bypasses the wrapper logic.
Returning projectedValue recursively causing infinite loop.
4fill in blank
hard

Fill both blanks to create a composed property wrapper TrimmedCapitalized that applies Trimmed and Capitalized wrappers.

Swift
@propertyWrapper struct TrimmedCapitalized {
    @Trimmed var trimmed: String
    @Capitalized var capitalized: String

    var wrappedValue: String {
        get { [1] }
        set {
            [2] = newValue
            capitalized = newValue
        }
    }

    init(wrappedValue: String) {
        self.trimmed = wrappedValue
        self.capitalized = wrappedValue
    }
}
Drag options to blanks, or click blank then click option'
Atrimmed
Bcapitalized
Cself.trimmed
Dself.capitalized
Attempts:
3 left
💡 Hint
Common Mistakes
Using just trimmed without self. which can cause ambiguity.
Setting capitalized before trimmed causing wrong order.
5fill in blank
hard

Fill all three blanks to complete the dictionary comprehension that creates a dictionary with keys as uppercased trimmed words and values as their capitalized versions.

Swift
let words = ["  hello", "world  ", " swift"]
let transformed = [
    [1]: [2] for word in words
    if word[3] " "
]
Drag options to blanks, or click blank then click option'
Aword.trimmingCharacters(in: .whitespaces).uppercased()
Bword.capitalized
C.contains
D.hasPrefix
Attempts:
3 left
💡 Hint
Common Mistakes
Using .hasPrefix instead of .contains for filtering.
Not trimming whitespace before uppercasing the key.
Using the raw word instead of capitalized for the value.