Challenge - 5 Problems
Swift Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple custom validation property wrapper
What is the output of this Swift code using a custom validation property wrapper?
Swift
import Foundation @propertyWrapper struct NonEmpty { private var value: String = "" var wrappedValue: String { get { value } set { if newValue.isEmpty { print("Empty string not allowed") } else { value = newValue } } } } struct User { @NonEmpty var name: String } var user = User() user.name = "" print(user.name) user.name = "Alice" print(user.name)
Attempts:
2 left
💡 Hint
Look at how the setter handles empty strings and what is printed.
✗ Incorrect
The setter prints a message when an empty string is assigned but does not change the stored value. Initially, the value is empty, so printing it shows an empty line. After assigning "Alice", the value changes and prints "Alice".
🧠 Conceptual
intermediate1:30remaining
Purpose of custom validation property wrappers
What is the main purpose of using a custom validation property wrapper in Swift?
Attempts:
2 left
💡 Hint
Think about what validation means in everyday life.
✗ Incorrect
Custom validation property wrappers help ensure that property values meet certain rules before being stored, like checking if a string is not empty.
🔧 Debug
advanced2:30remaining
Identify the error in this custom validation property wrapper
What error will this Swift code produce when compiled?
Swift
@propertyWrapper struct Positive { private var value: Int var wrappedValue: Int { get { value } set { if newValue > 0 { value = newValue } else { print("Value must be positive") } } } } struct Account { @Positive var balance: Int } let acc = Account() print(acc.balance)
Attempts:
2 left
💡 Hint
Check if all stored properties have initial values.
✗ Incorrect
The property wrapper's stored property 'value' is not initialized, causing a compile-time error.
📝 Syntax
advanced2:00remaining
Correct syntax for a property wrapper with projectedValue
Which option correctly defines a property wrapper with a projectedValue that returns a Bool indicating if the value is valid?
Swift
struct Length { private var value: String var wrappedValue: String { get { value } set { value = newValue } } var projectedValue: Bool { // returns true if length > 3 } }
Attempts:
2 left
💡 Hint
Use the correct property for string length and a simple expression.
✗ Incorrect
Option A correctly uses 'value.count' and returns a Bool expression. Other options have syntax errors or use invalid properties.
🚀 Application
expert3:00remaining
Using a custom validation property wrapper in a struct
Given this property wrapper, what will be the output of the following code?
Swift
@propertyWrapper struct Clamped { private var value: Int private let range: ClosedRange<Int> var wrappedValue: Int { get { value } set { value = min(max(newValue, range.lowerBound), range.upperBound) } } init(wrappedValue: Int, _ range: ClosedRange<Int>) { self.range = range self.value = min(max(wrappedValue, range.lowerBound), range.upperBound) } } struct Settings { @Clamped(0...10) var volume: Int = 5 } var s = Settings() s.volume = 15 print(s.volume) s.volume = -3 print(s.volume) s.volume = 7 print(s.volume)
Attempts:
2 left
💡 Hint
The wrapper clamps values to the range 0 to 10.
✗ Incorrect
The property wrapper limits the value to the range 0 to 10. Assigning 15 clamps to 10, -3 clamps to 0, and 7 stays as 7.