Observation Swift: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Observation in Swift: Complete Guide with Examples

1. What Is It and Why You Need It

Observation is a new framework introduced by Apple at WWDC 2023 that completely rethinks data observation in Swift. It replaces the outdated model based on @Published and ObservableObject from Combine, offering a more performant, thread-safe, and intuitive approach. At its core, Observation uses the Observable protocol and the @Observable macro, which track changes at the individual property level rather than the entire object.

Why does this matter? In modern iOS, macOS, watchOS, and tvOS development, SwiftUI is the primary framework for building interfaces. For SwiftUI to work efficiently, it needs a mechanism that notifies it of data changes so it can redraw only the parts of the interface that actually changed. The old approach with ObservableObject redrew the entire View whenever any property changed, leading to redundant computations and performance hits. Observation solves this by providing precise change tracking at the property level.

Key advantages: Observation is part of the Swift standard library (starting with iOS 17, macOS 14, tvOS 17, watchOS 10), requires no additional dependencies, is fully thread-safe (changes can be made from any thread, with notifications delivered on the main thread), and integrates tightly with SwiftUI through the @Bindable macro and the new .onChange(of:) modifier.

2. Installation

Observation is a built-in Apple framework. It's part of the Swift standard library starting with Swift 5.9 and Xcode 15. No additional installation via package managers (CocoaPods, Swift Package Manager, Carthage) is required.

To start using Observation, simply:

  1. Install Xcode 15 or later.
  2. In your project settings, ensure the minimum deployment target is set to iOS 17, macOS 14, tvOS 17, or watchOS 10.
  3. Import the framework in the file where you plan to use it: import Observation.

If you're using SwiftUI, you don't need to import Observation separately — it's automatically available when you import SwiftUI.

3. Quick Start — Minimal Working Example

Let's create a simple data model and a View that displays it.

import SwiftUI

// 1. Create an observable class
@Observable
class UserSettings {
    var username: String = "Guest"
    var isLoggedIn: Bool = false
}

// 2. Use it in a View
struct ContentView: View {
    // @State to manage the lifecycle
    @State private var settings = UserSettings()

    var body: some View {
        VStack(spacing: 20) {
            Text("User: \(settings.username)")
                .font(.title)

            TextField("Enter your name", text: $settings.username)
                .textFieldStyle(.roundedBorder)
                .padding()

            Toggle("Log In", isOn: $settings.isLoggedIn)
                .padding()

            if settings.isLoggedIn {
                Text("Logged in as \(settings.username)")
                    .foregroundColor(.green)
            } else {
                Text("You are not logged in")
                    .foregroundColor(.red)
            }
        }
        .padding()
    }
}

In this example, the @Observable macro automatically transforms the UserSettings class into an observable object. SwiftUI tracks changes to username and isLoggedIn individually, so only the relevant parts of the UI update when a property changes.

Recommendations