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.

Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

ToDoBar

Your to-dos on your menu bar.

Ram Patra Published on August 31, 2024
Image placeholder

Keep reading

If this article was helpful, others might be too

question swiftui swift September 8, 2024 How to make Squircle shape in SwiftUI and how to easily convert it to a circle or a rectangle?

To create a squircle shape (a combination of a square and a circle, also known as a superellipse) in SwiftUI, you can define a custom shape by conforming to the Shape protocol and implementing the superellipse formula. The formula for a superellipse is:

question swiftui macos October 15, 2023 How to display the app version and build number in a macOS/iOS SwiftUI app?

To display both the app version and build number in a SwiftUI macOS/iOS app, you can use the Bundle class to access information from the app’s Info.plist file. The Info.plist file contains various details about your application, including its version and build number. Here’s how you can do it:

question swiftui swift May 29, 2022 How to open a window in SwiftUI using NSWindowController?

Although many things in SwiftUI are idiomatic and straightforward, showing your view in a new window needs a bit of coding to do. Hence, this short post.

Like my work?

Please, feel free to reach out. I would be more than happy to chat.