0
0
Swiftprogramming~10 mins

Property wrappers with configuration in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Property wrappers with configuration
Define Property Wrapper
Add Configurable Parameter
Use Wrapper with Parameter
Access Wrapped Property
Observe Behavior Based on Config
This flow shows how to define a property wrapper with a parameter, use it on a property, and see how the configuration affects the property's behavior.
Execution Sample
Swift
@propertyWrapper
struct Clamped {
  private var value: Int
  let range: ClosedRange<Int>

  init(wrappedValue: Int, _ range: ClosedRange<Int>) {
    self.range = range
    self.value = min(max(wrappedValue, range.lowerBound), range.upperBound)
  }

  var wrappedValue: Int {
    get { value }
    set { value = min(max(newValue, range.lowerBound), range.upperBound) }
  }
}

struct Player {
  @Clamped(0...100) var health: Int = 50
}

var p = Player()
p.health = 120
print(p.health)
This code defines a property wrapper 'Clamped' that limits an integer to a range, then uses it to clamp a player's health between 0 and 100.
Execution Table
StepActionValue of 'health'RangeOutput
1Initialize Player with health=50500...100
2Set health to 120 (outside range)1000...100
3Print health1000...100100
4Set health to -10 (below range)00...100
5Print health00...1000
6Set health to 75 (within range)750...100
7Print health750...10075
8End of executionExecution stops
💡 Execution stops after demonstrating clamping behavior on health property.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
health5010007575
range0...1000...1000...1000...1000...100
Key Moments - 3 Insights
Why does setting health to 120 result in 100 instead of 120?
Because the property wrapper clamps the value to the maximum of the range 0...100, as shown in execution_table step 2.
What happens if we set health to a value below the range, like -10?
The wrapper clamps it to the minimum of the range, 0, as seen in execution_table step 4.
Does the initial value of health get clamped too?
Yes, the initializer clamps the wrappedValue, so the initial 50 is within range and stays 50 (step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'health' after setting it to 120?
A100
B120
C0
D50
💡 Hint
Check step 2 in the execution_table where health is set to 120 and clamped.
At which step does the health value become 0?
AStep 6
BStep 2
CStep 4
DStep 1
💡 Hint
Look at step 4 in the execution_table where health is set below the range.
If the range was changed to 10...90, what would be the health value after setting it to 5?
A5
B10
C90
D0
💡 Hint
Refer to how clamping works in the variable_tracker and execution_table for values outside the range.
Concept Snapshot
@propertyWrapper struct WrapperName {
  var wrappedValue: Type
  init(wrappedValue: Type, config: ConfigType) { ... }
  var wrappedValue: Type { get set }
}

Use with @WrapperName(config) var property

The wrapper can clamp, validate, or modify the property using config parameters.
Full Transcript
This example shows how to create a Swift property wrapper called Clamped that limits an integer property to a specified range. The wrapper takes a range as a configuration parameter. When the property is set, the wrapper clamps the value to stay within the range. The Player struct uses this wrapper to keep health between 0 and 100. The execution table traces setting health to values inside and outside the range, showing how the wrapper adjusts the stored value accordingly. Key moments clarify why values are clamped and how the initial value is handled. The visual quiz tests understanding of clamping behavior and configuration effects.