Loading...

@Published in SwiftUI

question swiftui swift
Ram Patra Published on August 31, 2024

In SwiftUI, the @Published property wrapper is used in combination with the ObservableObject protocol to automatically announce changes to properties of a class. This allows SwiftUI views that depend on these properties to update automatically when the data changes.

Here’s how @Published works:

  1. Declaration: When you mark a property with @Published, you’re indicating that changes to this property should be observed.

  2. ObservableObject: The class containing @Published properties must conform to the ObservableObject protocol. This protocol allows the class to broadcast that a property has changed.

  3. View Updates: Views that use this ObservableObject can observe changes by using the @ObservedObject or @StateObject property wrappers. When a @Published property changes, any view that depends on it will automatically re-render.

Example

import SwiftUI
import Combine

class UserSettings: ObservableObject {
    @Published var username: String = "Anonymous"
}

struct ContentView: View {
    @ObservedObject var settings = UserSettings()

    var body: some View {
        VStack {
            Text("Hello, \(settings.username)!")
            Button("Change Username") {
                settings.username = "New Username"
            }
        }
    }
}

Key Points

  • Automatic Updates: When settings.username changes, the ContentView automatically updates to reflect the new username.
  • Publisher: @Published creates a publisher for the property, allowing subscribers (like views) to react to changes.
  • Combine Integration: Behind the scenes, @Published integrates with the Combine framework, leveraging its capabilities to handle changes in data streams.

In essence, @Published is a way to bind data in a way that ensures the UI remains in sync with the underlying data model.

Ram Patra Published on August 31, 2024
Image placeholder

Keep reading

If this article was helpful, others might be too

question swiftui swift September 7, 2024 How to apply mirroring to any SwiftUI view?

You can apply mirroring to a SwiftUI view by using the scaleEffect(x:y:anchor:) modifier to flip the view horizontally or vertically. Specifically, you can set the x or y scale to -1.0 to mirror the view along that axis.

question swiftui iOS April 2, 2024 How to force an app or a view to open in landscape only mode in iOS using SwiftUI?

In SwiftUI, you can force an app to open in landscape mode by configuring the supported interface orientations in your app’s target settings. Here’s a step-by-step guide to configuring interface orientations in Xcode:

question swiftui macos September 3, 2024 How to open and close windows programmatically in SwiftUI?

To open or close a window programmatically from outside that window using environment variables, you need to leverage the new openWindow (macOS 13+) and dismissWindow (macOS 14+) environment variables. This environment variables allow you to programmatically open and close a window by its identifier.