How can I observe changes on an array with Combine framework but without SwiftUI?
Basically, I want this sink block to get called when an element is added or removed.
import Combine
var list = ["A", "B", "C"]
list.publisher
.collect()
.sink { value in
print(value)
}
// I want to observe these changes.
list.append("D")
list.removeAll { $0 == "B"}
I read this article and I know @Published property wrapper works this way.
But I can't use SwiftUI and am looking for another solution.
AnyCancellableandAnyPublisher. BTW@Publishedworks with SwiftUI but it is aCombinewrapper.AnyCancellable. Subscriptions return an instance ofAnyCancellableas a “cancellation token,” .In above example yoursubscriptionwill becancelledautomatically after collecting all values, and ignoring other commands like append. Check stackoverflow.com/questions/63543450/…