17 Nov 2023

@Published wrapper in SwiftUI

@Published is a property wrapper in SwiftUI that allows us to create observable objects that automatically announce when changes occur.

Whenever the property is changed, all vies using that object will be reloaded to reflect those changes.

class Bag: ObservableObkect {
    var items = [String]()
}

This conforms to the ObservableObject protocol (which means views can watch it for changes. But since items isn’t marked with @Published, no change announcements will be sent.

class Bag: ObservableObject {
    @Published var items = [String]()
}