Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The property wrapper modifies the new value by applying the
capitalized method to reduce repetitive capitalization code.2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong wrapper name or forgetting the '@' symbol.
Confusing capitalization with other string transformations.
✗ Incorrect
The
@Capitalized wrapper is applied to the name property to automatically capitalize it.3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name other than
wrappedValue.Not initializing the property wrapper's value properly.
✗ Incorrect
The initializer must accept
wrappedValue to initialize the property wrapper correctly.4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Reversing the order of trimming and capitalization.
Using incorrect string methods.
✗ Incorrect
First trim whitespace, then capitalize the string to reduce repetitive code.
5fill in blank
hardFill 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'
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.
✗ Incorrect
The code trims whitespace, capitalizes the string, and then limits it to 10 characters to reduce repetitive code.