I have a custom Publisher that takes in a String key and emits values when data for that key changes. Say it's called MyCustomPublisher
.
I have a function that I want to take in an instance of MyCustomPublisher
, transform the value into a different key, and then create a separate instance of MyCustomPublisher
. This looks like this:
let myInfoProvider: MyCustomPublisher = ...
myInfoProvider.tryMap { v -> Database in
guard let value = v else { throw "No value provided" }
return value
}
.map { v in
MyCustomPublisher(key: v)
}
.switchToLatest()
.tryMap { v -> String in
guard let value = v else {
throw "No value found"
}
return value
}
Given that the data is a String, I would like the output type to be AnyPublisher<String, Error>
, but the actual type reported by Swift is Publishers.TryMap<Publishers.SwitchToLatest<MyCustomPublisher, Publishers.TryMap<MyCustomPublisher, MyCustomPublisher>>, String>
. How can I make this conform to AnyPublisher<String, Error>?
You would just call eraseToAnyPublisher()
on the result.