← All courses

State and Binding

🗓 May 31, 2026 ⏱ 1 min read

@State — local data

Mark a value with @State and SwiftUI watches it. Change it, and the view redraws.

struct ToggleView: View {
    @State private var isOn = false
    var body: some View {
        Toggle("Notifications", isOn: $isOn)  // $ creates a binding
    }
}

@Binding — share state with a child

struct Child: View {
    @Binding var text: String
    var body: some View {
        TextField("Name", text: $text)
    }
}

ObservableObject — shared app data

class Cart: ObservableObject {
    @Published var items: [String] = []
}

struct CartView: View {
    @StateObject var cart = Cart()
    var body: some View { Text("\(cart.items.count) items") }
}
Remember: The $ sign turns a value into a two-way binding the child can edit.