0
0
Swiftprogramming~10 mins

Custom validation property wrappers in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom validation property wrappers
Define Property Wrapper
Add Validation Logic
Apply Wrapper to Property
Set Property Value
Validate Value Automatically
Accept or Reject Value
This flow shows how a custom property wrapper is defined with validation logic, applied to a property, and automatically validates values when set.
Execution Sample
Swift
@propertyWrapper
struct NonEmpty {
  private var value: String = ""
  var wrappedValue: String {
    get { value }
    set {
      if newValue.isEmpty { print("Error: Empty string") } else { value = newValue }
    }
  }
}

struct User {
  @NonEmpty var name: String
}

var user = User()
user.name = "Alice"
user.name = ""
This code defines a property wrapper that validates a string is not empty, then applies it to a User's name property and tests setting valid and invalid values.
Execution Table
StepActionValue SetValidation CheckResultOutput
1Create User instance--name = ""-
2Set user.name = "Alice""Alice"Is empty? NoValue accepted-
3Set user.name = """"Is empty? YesValue rejectedError: Empty string
💡 Execution stops after last assignment; invalid empty string rejected by validation.
Variable Tracker
VariableStartAfter Step 2After Step 3
user.name"""Alice""Alice" (unchanged due to validation)
Key Moments - 2 Insights
Why does user.name not become empty after setting it to ""?
Because the validation in the property wrapper rejects empty strings (see execution_table step 3), so the value remains the previous valid string.
When is the validation logic triggered?
Validation runs every time the wrapped property is set (see execution_table steps 2 and 3), automatically inside the setter of wrappedValue.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of user.name after step 3?
A"" (empty string)
B"Alice"
Cnil
DError
💡 Hint
Check the 'Result' and 'Value Set' columns in step 3 showing rejection and unchanged value.
At which step does the validation detect an invalid value?
AStep 1
BStep 2
CStep 3
DNo validation occurs
💡 Hint
Look at the 'Validation Check' column for when the empty string is detected.
If the validation allowed empty strings, what would change in variable_tracker after step 3?
Auser.name would be ""
Buser.name would be "Alice"
Cuser.name would be nil
DNo change
💡 Hint
Refer to variable_tracker showing value remains "Alice" due to rejection.
Concept Snapshot
@propertyWrapper struct WrapperName {
  private var value: Type
  var wrappedValue: Type {
    get { value }
    set {
      // validation logic here
      if valid(newValue) { value = newValue }
    }
  }
}

Use @WrapperName on properties to auto-validate on set.
Full Transcript
This visual trace shows how to create a custom validation property wrapper in Swift. First, the wrapper struct is defined with a private stored value and a wrappedValue property that runs validation in its setter. When applied to a property, any assignment triggers the validation automatically. In the example, setting user.name to "Alice" passes validation and updates the value. Setting it to an empty string fails validation, prints an error, and keeps the old value. The variable tracker confirms the value remains unchanged after invalid input. This helps ensure properties always hold valid data without extra checks outside the wrapper.