Home > AI > IOS > Combine >

setFailureType(to:)

Example (Apple Official): pub1’s failure type is Never while pub2’s failure type is Error, which are inconsistent. So we need use setFailureType to make them consistent and we can use combineLatest later.

let pub1 = [0, 1, 2, 3, 4, 5].publisher
let pub2 = CurrentValueSubject<Int, Error>(0)
let cancellable = pub1
    .setFailureType(to: Error.self)
    .combineLatest(pub2)
    .sink(
        receiveCompletion: { print ("completed: \($0)") },
        receiveValue: { print ("value: \($0)")}
     )

Leave a Reply