Reputation: 325
I have a property with a fixed type
var statusPublisher: Published<Status>.Publisher
and some other statuses like substatus1
, substatus2
and I want it to combine values of other statuses like
var statusPublisher: Published<Status>.Publisher {
$substatus1.combineLatest($substatus2).map { s1, s2 -> Status in ... }
}
but it says Cannot convert return expression of type 'Publishers.CombineLatest<Published<Substatus1>.Publisher, Published<Substatus2>.Publisher>' to return type 'Published<Status>.Publisher'
I managed to workaround it with one extra
var connectionStatusSubject = PassthroughSubject<Status, Never>()
that I put into
var connectionStatusPublisher: AnyPublisher<Status, Never> {
connectionStatusSubject.eraseToAnyPublisher()
}
with new AnyPublisher
here, but it seems not so neat.
Is there any way to make it the right way?
Upvotes: 1
Views: 395
Reputation: 49590
Published<T>.Publisher
is just a specific type of a publisher, created by @Published
property wrapper. There's no reason at all to use that as the type of the computed property.
Type-erase to AnyPublisher
- like you did with PassthroughSubject
- except you don't need a PassthroughSubject
:
var statusPublisher: AnyPublisher<Status, Never> {
$substatus1
.combineLatest($substatus2)
.map { s1, s2 -> Status in ... }
.eraseToAnyPublisher()
}
Upvotes: 2