0
0
Swiftprogramming~10 mins

Composing property wrappers in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Composing property wrappers
Define Wrapper A
Define Wrapper B
Apply Wrapper B on Wrapper A
Use Composed Wrapper on Property
Access Property
Wrapper B logic runs
Wrapper A logic runs
Final value returned
This flow shows how two property wrappers are defined and then combined by applying one wrapper on another, so when the property is accessed, the wrappers run in order.
Execution Sample
Swift
@propertyWrapper
struct WrapperA {
  var wrappedValue: Int
  init(wrappedValue: Int) {
    self.wrappedValue = wrappedValue
  }
}

@propertyWrapper
struct WrapperB {
  var inner: WrapperA
  init(wrappedValue: WrapperA) {
    self.inner = wrappedValue
  }
  var wrappedValue: Int {
    get { inner.wrappedValue }
    set { inner.wrappedValue = newValue }
  }
}

@WrapperB @WrapperA var number = 10
print(number)
This code defines two simple wrappers and composes them on a property, then prints the final value.
Execution Table
StepActionWrapper InvolvedValue BeforeValue AfterNotes
1Initialize WrapperA with 10WrapperAN/A10WrapperA stores 10
2Initialize WrapperB with WrapperA instanceWrapperBN/AWrapperA instanceWrapperB wraps WrapperA
3Access property 'number'WrapperBWrapperA instanceCalls WrapperB.wrappedValue getterWrapperB getter runs first
4WrapperB getter calls WrapperA getterWrapperA1010WrapperA getter returns 10
5WrapperB getter returns value 10WrapperB1010Final value is 10
6Print valueNone1010Output is 10
💡 Property accessed and wrappers executed in order, final value 10 returned
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
WrapperA.wrappedValueN/A10101010
WrapperB.wrappedValueN/AN/AWrapperA instance1010
numberN/AN/AN/A1010
Key Moments - 3 Insights
Why does WrapperB's getter run before WrapperA's getter when accessing the property?
Because WrapperB is applied outermost, it wraps WrapperA, so accessing the property calls WrapperB's getter first, which then calls WrapperA's getter (see execution_table steps 3 and 4).
What value does WrapperA store initially?
WrapperA stores the initial value 10 directly when initialized (see execution_table step 1).
How does the final printed value relate to the wrappers?
The final printed value is the result after both wrappers' getters run, which is 10 here (see execution_table step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what value does WrapperA hold after initialization (Step 1)?
A10
BWrapperB instance
CN/A
D0
💡 Hint
Check the 'Value After' column in Step 1 of the execution_table.
At which step does WrapperB's getter call WrapperA's getter?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the step where WrapperA's getter returns a value inside WrapperB's getter (execution_table).
If we reversed the order of wrappers to @WrapperA @WrapperB, which wrapper's getter runs first?
AWrapperB
BWrapperA
CBoth run simultaneously
DNeither runs
💡 Hint
Recall that the outermost wrapper's getter runs first; reversing order changes which is outermost.
Concept Snapshot
@propertyWrapper struct WrapperA { var wrappedValue: Int init(wrappedValue: Int) { self.wrappedValue = wrappedValue } }
@propertyWrapper struct WrapperB { var wrappedValue: Int }
Use @WrapperB @WrapperA var x = 10 to compose wrappers.
Accessing x calls WrapperB's getter, which calls WrapperA's getter.
Wrappers run outer to inner, layering behavior on the property.
Full Transcript
This example shows how to compose two property wrappers in Swift. WrapperA and WrapperB are defined as simple wrappers holding an Int. When we apply @WrapperB @WrapperA to a property, WrapperB wraps WrapperA. Accessing the property calls WrapperB's getter first, which then calls WrapperA's getter, returning the stored value 10. The execution table traces each step: initializing wrappers, accessing the property, and printing the final value. The variable tracker shows how values change or stay the same through the steps. Key moments clarify why the outer wrapper runs first and how values flow. The quiz tests understanding of wrapper order and value flow. This helps beginners see how composing wrappers layers behavior on properties in Swift.