say I have an array of Person structs like this:
struct Person {
var name: String
}
var persons = [Person(name: "A"), Person(name: "B"), Person(name: "C")]
let publisher = CurrentValueSubject<[Person], Never>(persons)
// subscribe to changes to the array
let subscription: AnyCancellable = publisher.sink { (persons) in
print("The array has changed")
}
var currentPersons = publisher.value
currentPersons[0].name = "Changed"
publisher.send(currentPersons)
This works. It publishes every time anything in the array changes or if something is deleted or added.
But what if I wanted to additionally be able to subscribe to only one element in the array? Can I do that? I simply can't figure it out.
Personobservable, by exposing its properties asCurrentValueSubjectspersonsarray, instead of tracking one single instance of itContactsobject or whatever, and make it the unique owner of the array.