0
0
Swiftprogramming~10 mins

@propertyWrapper declaration in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @propertyWrapper declaration
Define @propertyWrapper struct
Add wrappedValue property
Use @propertyWrapper on variable
Access variable
wrappedValue getter/setter runs
Variable value updated or retrieved
This flow shows how a @propertyWrapper is declared, then used on a variable, and how accessing that variable runs the wrapper's getter and setter.
Execution Sample
Swift
import SwiftUI
@propertyWrapper
struct Capitalized {
  private var value: String = ""
  var wrappedValue: String {
    get { value }
    set { value = newValue.capitalized }
  }
}

struct Person {
  @Capitalized var name: String
}

var p = Person()
p.name = "john doe"
print(p.name)
This code defines a @propertyWrapper that capitalizes a string, uses it on a variable, sets a lowercase name, and prints the capitalized result.
Execution Table
StepActionVariable/PropertyValue BeforeValue AfterOutput
1Define @propertyWrapper struct Capitalized----
2Create Person instance pp---
3Set p.name = "john doe"p.name (wrappedValue setter)"""John Doe"-
4Get p.name for printp.name (wrappedValue getter)"John Doe""John Doe""John Doe"
5Print output---John Doe
💡 All steps complete, program ends after printing capitalized name.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
p.name (wrappedValue)"""John Doe""John Doe""John Doe"
p._name.value (private storage)"""John Doe""John Doe""John Doe"
Key Moments - 2 Insights
Why does setting p.name to "john doe" store "John Doe" instead?
Because the wrappedValue setter capitalizes the newValue before storing it, as shown in execution_table step 3.
What happens when we access p.name to print it?
The wrappedValue getter returns the stored capitalized string without change, as shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 3, what is the value stored after setting p.name?
A"john doe"
B"John Doe"
C"JOHN DOE"
D"john Doe"
💡 Hint
Check the 'Value After' column in step 3 of execution_table.
At which step does the wrappedValue getter run?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for 'wrappedValue getter' in the Action column of execution_table.
If we remove the capitalization in the setter, what would p.name print?
A"JOHN DOE"
B"John Doe"
C"john doe"
D"john Doe"
💡 Hint
Consider how the setter changes the value before storing it, see execution_table step 3.
Concept Snapshot
@propertyWrapper declaration syntax:
@propertyWrapper struct WrapperName {
  var wrappedValue: Type { get set }
}

Use by prefixing variable with @WrapperName.
Accessing variable calls wrappedValue getter/setter.
Allows custom behavior on property access.
Full Transcript
This visual trace shows how a Swift @propertyWrapper is declared and used. First, the wrapper struct Capitalized is defined with a wrappedValue property that capitalizes any new value set. Then, a Person struct uses @Capitalized on its name property. When we create a Person instance and set p.name to "john doe", the setter capitalizes it to "John Doe" before storing. When we print p.name, the getter returns the stored capitalized string. The execution table tracks each step, showing how the wrappedValue getter and setter run and how the value changes. Key moments clarify why the value is capitalized and how access works. The quiz tests understanding of these steps. The snapshot summarizes the syntax and behavior of @propertyWrapper declarations.