The article you linked to is based on Combine. The specific error you’re getting is because you’re building a Combine publisher pipeline where the terminal assign(to:)
method requires a publisher, and you don’t have a publisher.
It’s not clear why you’re trying to move from Combine to Observation, given that you have existing Combine code that works. However, if you do want to make that change then you need to look at it holistically. What is this code trying to do? And what’s the best way to accomplish that with Observation? The answer to that first question is:
-
publisher(for:)
uses KVO to observe the timeControlStatus
property.
-
.receive(on:)
ensures that any changes are received on the main thread.
-
.map(_:)
converts the stream of state values to a stream of Booleans, based on whether the state is .playing
.
-
assign(to:)
assigns new values to the isPlaying
property.
You have a couple of approaches for switching to Observation:
The latter will be easier, obviously (-: To do that, change the assign(to:)
to a sink(receiveValue:)
where the closure sets self.isPlaying
.
IMPORTANT sink(receiveValue:)
returns an AnyCancellable
. You must retain that to keep you Combine pipeline active.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"