Whenever you need to match failure types in Combine, for a Never
failure type, like a Just
publisher, you'd use setFailureType(to:)
:
let p: AnyPublisher<Int, Never> = ...
let p1 = p.setFailureType(to: Error.self)
.eraseToAnyPublisher() // AnyPublisher<Int, Error>
For a non-Never
failure, you'd need to use .mapError
:
let p2 = p1.mapError { CustomError(wrapping: $0) }
.eraseToAnyPublisher() // AnyPublisher<Int, CustomError>
So, in your case, if you want to change foo
's return value, you'd do:
func foo() -> AnyPublisher<Int, Never> {
Result<Int, Never>
.success(1).publisher
.setFailureType(to: Error.self)
.eraseToAnyPublisher()
}
And if you don't want to change foo
, but still use with with bar
, you can do:
Publishers.CombineLatest(foo().setFailureType(to: Error.self), boo())
.map { (f, b) in
// f and b are Ints, as emitted by foo and bar, respectively
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…