how can we notify observableobject about changes to its initializers?

Having a top-level property (outside of any class or struct) is probably not a good idea. I don’t see the whole picture, but it looks like your app needs a global state (e.g., a @StateObject initialised on the App level). Consider this answer:

If you really need to observe your array, you need to make it observable.

One option is to use CurrentValueSubject from the Combine framework:

var persons = ["Bob", "Nik", "Tak", "Sed", "Ted"].map(PersonData.init)
var allData = CurrentValueSubject<[PersonData], Never>(persons)

class PersonDataModel: ObservableObject {
    @Published var persones: [PersonData] = allData.value

    private var cancellables = Set<AnyCancellable>()

    init() {
            .sink { [weak self] in
                self?.persones = $0
            .store(in: &cancellables)

struct ContentView: View {
    @StateObject var personDataModel = PersonDataModel()

    var body: some View {
        VStack {
            Button("update allData") { 
                allData.send([PersonData(name: "Bob")]) 
            HStack {
                ForEach(personDataModel.persones) { person in 

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top