Reputation: 1430
I'm having trouble with a BehaviorRelay
that has a protocol type and using it on concrete types. Here's my code:
protocol Item {
var title: { get }
}
struct Can: Item {
let title = "Can"
}
let canRelay = BehaviorRelay<Can?>(value: nil)
func handle(item: BehaviorRelay<Item?>) {
// do something with item here
}
handle(item: canRelay) // can't do this?
I assumed I would be able to call handle(item:)
but it's not the case because the arguments don't match. I get that they don't match, but Can
is a type of Item
so shouldn't this be possible?
Upvotes: 1
Views: 423
Reputation: 33967
Can
may be a subtype of Item
, but BehaviorRelay<Can>
is not a subtype of BehaviorRelay<Item>
.
Also, you should not be passing BehaviorRelays around in code. Pass Observables instead.
Once you know these two rules, you end up with:
func handle(item: Observable<Item?>) {
// do something with item here
}
handle(item: canRelay.map { $0 })
Upvotes: 3