Here's the class:

class DataManager {
    static let shared = DataManager()

    var data: String = "Initial"
        
    private init() {}

    func updateData(newData: String) {
        self.data = newData
    }
}

let sharedManager = DataManager.shared

I update the data property in one view based with an onTapGesture in that view:

sharedManager.updateData(newData: "New")

When I check the value of sharedManager.data in that view, it is updated.

But when I look at sharedManager.data in a different view, it is not updated. How can I update that code to get it to work as expected: Update a property of an instance of a class in one view, have the update show in another?

1

There are 1 best solutions below

1
On BEST ANSWER

SwiftUI may not be updating the other views because your class is not set up for observation.

Try adding the @Observable macro:

@Observable
class DataManager {
...