0
0
Swiftprogramming~10 mins

Built-in property wrappers (@State, @Published) in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Built-in property wrappers (@State, @Published)
Declare property with @State/@Published
Property wrapper manages storage
Value changes via UI or code
Wrapper detects change
Triggers UI update or notifies observers
UI refreshes or subscribers react
This flow shows how @State and @Published wrap a property to track changes and update UI or notify observers automatically.
Execution Sample
Swift
import SwiftUI

struct ContentView: View {
  @State private var count = 0

  var body: some View {
    Button("Tap: \(count)") { count += 1 }
  }
}
A SwiftUI view with a @State property that updates the button label when tapped.
Execution Table
StepActionProperty ValueWrapper Detects ChangeUI Update TriggeredOutput
1Initialize count with 00NoNoButton shows 'Tap: 0'
2User taps buttoncount += 1 → 1YesYesButton updates to 'Tap: 1'
3User taps button againcount += 1 → 2YesYesButton updates to 'Tap: 2'
4No further taps2NoNoButton remains 'Tap: 2'
💡 No more user interaction; UI stable with count = 2
Variable Tracker
VariableStartAfter 1After 2Final
count0122
Key Moments - 3 Insights
Why does the UI update automatically when count changes?
Because @State wraps count and detects changes (see execution_table steps 2 and 3), it triggers the UI to refresh automatically.
What happens if we change count without @State?
Without @State, changes to count won't notify SwiftUI to update the UI, so the button label won't change (not shown in table but implied).
How is @Published different from @State?
@Published is used in ObservableObject classes to notify external subscribers, while @State is for local view state; both detect changes and trigger updates.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of count after step 2?
A1
B0
C2
DUndefined
💡 Hint
Check the 'Property Value' column at step 2 in the execution_table.
At which step does the UI update get triggered for the first time?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'UI Update Triggered' column in the execution_table.
If the user never taps the button, what will the final count value be?
A1
B0
C2
DCannot tell
💡 Hint
Refer to the 'Property Value' at step 1 and the exit_note in the execution_table.
Concept Snapshot
@State and @Published wrap properties to track changes.
@State is for local view state in SwiftUI.
@Published is for ObservableObject classes.
When value changes, UI or subscribers update automatically.
Use them to keep UI in sync with data changes.
Full Transcript
This visual trace shows how Swift's built-in property wrappers @State and @Published work. When you declare a property with @State, it wraps the value and watches for changes. For example, in the sample code, the count starts at 0. When the user taps the button, count increases by 1. The @State wrapper detects this change and triggers the UI to update, so the button label changes to show the new count. This process repeats with each tap. If the property was not wrapped, the UI would not update automatically. @Published works similarly but is used in ObservableObject classes to notify external listeners. This trace helps beginners see how these wrappers manage state and UI updates step-by-step.