Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a property wrapper named ValidateNonEmpty that checks if a string is not empty.
Swift
import Foundation @propertyWrapper struct ValidateNonEmpty { private var value: String = "" var wrappedValue: String { get { value } set { if newValue.isEmpty { value = "[1]" } else { value = newValue } } } init(wrappedValue: String) { self.wrappedValue = wrappedValue } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving the value empty instead of assigning a default invalid string.
Not handling the empty string case in the setter.
✗ Incorrect
The property wrapper sets the value to "invalid" if the new string is empty, ensuring validation.
2fill in blank
mediumComplete the code to add a validation check that the wrapped string has at least 5 characters.
Swift
import Foundation @propertyWrapper struct ValidateLength { private var value: String = "" var wrappedValue: String { get { value } set { if newValue.count < [1] { value = "Too short" } else { value = newValue } } } init(wrappedValue: String) { self.wrappedValue = wrappedValue } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number smaller or larger than 5, which breaks the intended validation.
Confusing the comparison operator.
✗ Incorrect
The validation requires the string to have at least 5 characters, so the check uses 5.
3fill in blank
hardFix the error in the property wrapper to correctly validate that the wrapped value is always uppercase.
Swift
import Foundation @propertyWrapper struct ValidateUppercase { private var value: String = "" var wrappedValue: String { get { value } set { if newValue == newValue.[1]() { value = newValue } else { value = newValue.uppercased() } } } init(wrappedValue: String) { self.wrappedValue = wrappedValue } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercased() which checks for lowercase instead.
Using capitalized() which capitalizes only the first letter.
✗ Incorrect
To check if the string is uppercase, compare it to newValue.uppercased().
4fill in blank
hardFill both blanks to create a property wrapper that validates an integer is within a range 1 to 100.
Swift
import Foundation @propertyWrapper struct ValidateRange { private var value: Int = 1 var wrappedValue: Int { get { value } set { if newValue [1] 1 && newValue [2] 100 { value = newValue } else { value = 1 } } } init(wrappedValue: Int) { self.wrappedValue = wrappedValue } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or < which exclude boundary values.
Reversing the operators.
✗ Incorrect
The validation checks if newValue is greater than or equal to 1 and less than or equal to 100.
5fill in blank
hardFill all three blanks to create a property wrapper that validates a string contains only letters and is capitalized.
Swift
import Foundation @propertyWrapper struct ValidateLettersCapitalized { private var value: String = "" var wrappedValue: String { get { value } set { let letters = CharacterSet.[1] if newValue.rangeOfCharacter(from: letters.inverted) == nil && newValue == newValue.[2]() { value = newValue } else { value = newValue.[3]() } } } init(wrappedValue: String) { self.wrappedValue = wrappedValue } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong CharacterSet like alphanumerics.
Confusing capitalized() with uppercased().
Setting value to capitalized() instead of lowercased() on invalid input.
✗ Incorrect
The code uses CharacterSet.letters to check only letters, compares with capitalized(), and sets value to lowercased() if invalid.