0
0
Swiftprogramming~10 mins

WrappedValue and projectedValue in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - WrappedValue and projectedValue
Declare property with @propertyWrapper
Access property.wrappedValue
Get or set the wrapped value
Access property.projectedValue
Get extra info or helper from wrapper
This flow shows how a property wrapper manages a property’s value via wrappedValue and offers extra info via projectedValue.
Execution Sample
Swift
@propertyWrapper
struct Capitalized {
  private var value: String = ""
  var wrappedValue: String {
    get { value }
    set { value = newValue.capitalized }
  }
  var projectedValue: String { "Length: \(value.count)" }
}

struct Person {
  @Capitalized var name: String
  
  init(name: String) {
    self.name = name
  }
}

var p = Person(name: "john")
print(p.name)          // John
print(p.$name)         // Length: 4
This code shows a property wrapper that capitalizes a string and provides its length as projectedValue.
Execution Table
StepActionwrappedValueprojectedValueOutput
1Create Person with name "john""John"Length: 4
2Access p.name (wrappedValue getter)"John"Length: 4John
3Access p.$name (projectedValue getter)"John"Length: 4Length: 4
4Set p.name = "alice""Alice"Length: 5
5Access p.name after set"Alice"Length: 5Alice
6Access p.$name after set"Alice"Length: 5Length: 5
7End of executionExecution stops
💡 Execution stops after demonstrating wrappedValue and projectedValue access and update.
Variable Tracker
VariableStartAfter 1After 4Final
value (inside Capitalized)"""John""Alice""Alice"
wrappedValue"""John""Alice""Alice"
projectedValue"Length: 0""Length: 4""Length: 5""Length: 5"
Key Moments - 3 Insights
Why does p.name print "John" with capital J even though we initialized with "john"?
Because the wrappedValue setter capitalizes the string before storing it, as shown in execution_table step 1 and 4.
What is p.$name and why is it different from p.name?
p.$name accesses the projectedValue, which provides extra info (string length here), shown in execution_table steps 3 and 6.
If we change p.name, does projectedValue update automatically?
Yes, projectedValue reflects the current wrappedValue state, updated after each set, as seen in steps 4 to 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the wrappedValue after step 4?
A"John"
B"alice"
C"Alice"
D"ALICE"
💡 Hint
Check the wrappedValue column at step 4 in the execution_table.
At which step does accessing projectedValue first print "Length: 4"?
AStep 2
BStep 3
CStep 1
DStep 5
💡 Hint
Look at the projectedValue column and output at step 3 in the execution_table.
If we set p.name = "bob", what would p.$name output be?
A"Length: 3"
B"Length: 4"
C"Length: 5"
D"Length: 0"
💡 Hint
Refer to variable_tracker projectedValue updates after setting wrappedValue.
Concept Snapshot
@propertyWrapper defines wrappedValue to get/set the main value.
projectedValue provides extra info accessed via $property.
Setting wrappedValue triggers custom logic (e.g., capitalization).
Accessing projectedValue gives helper data (e.g., length).
Use p.name for value, p.$name for extra info.
Full Transcript
This visual trace shows how Swift property wrappers use wrappedValue and projectedValue. The wrappedValue is the main property value, here a capitalized string. The projectedValue offers extra info, here the string length. When creating a Person with name "john", the wrappedValue setter capitalizes it to "John". Accessing p.name gets the capitalized name. Accessing p.$name gets the length string. Changing p.name updates both wrappedValue and projectedValue accordingly. This helps understand how property wrappers manage data and provide helpers.