Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The
wrappedValue setter should capitalize the new string value, so .capitalized is the correct method.2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated wrappers like @Lazy or @State which do not trim strings.
Applying wrappers in the wrong order.
✗ Incorrect
The
Trimmed wrapper should be applied before Capitalized to trim whitespace before capitalizing.3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning
value directly which bypasses the wrapper logic.Returning
projectedValue recursively causing infinite loop.✗ Incorrect
The
projectedValue should delegate to wrappedValue to expose the trimmed string.4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using just
trimmed without self. which can cause ambiguity.Setting
capitalized before trimmed causing wrong order.✗ Incorrect
The getter and setter should use
self.trimmed to get and set the trimmed value before capitalizing.5fill in blank
hardFill 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'
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.
✗ Incorrect
The key is the trimmed and uppercased word, the value is the capitalized word, and the filter checks if the word contains a space.