0
0
Swiftprogramming~10 mins

Custom validation 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 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'
A"invalid"
B"empty"
C"default"
D"none"
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.
2fill in blank
medium

Complete 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'
A3
B10
C5
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number smaller or larger than 5, which breaks the intended validation.
Confusing the comparison operator.
3fill in blank
hard

Fix 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'
Alowercased
Bcapitalized
CtrimmingCharacters
Duppercased
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercased() which checks for lowercase instead.
Using capitalized() which capitalizes only the first letter.
4fill in blank
hard

Fill 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'
A>=
B<=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or < which exclude boundary values.
Reversing the operators.
5fill in blank
hard

Fill 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'
Aletters
Bcapitalized
Dlowercased
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.