Swift combine error: method on 'Publisher' requires .Failure' (aka 'WeatherError') and 'Never' be equivalent

Viewed 6784

I am learning Swift Combine now, found quite easy video tutorial, however for some reason I get error when I try to use my enum in PassthroughSubject<Int, WeatherError>()

Check this code:

import Combine 

enum WeatherError: Error {
   case thingsJustHappen
   
}
let weatherPublisher = PassthroughSubject<Int, WeatherError>()


let subscriber = weatherPublisher
   .filter {$0 > 10}
   .sink { value in
       print("\(value)")
   }

weatherPublisher.send(10)
weatherPublisher.send(30)

".filter" is highlighted and the error is:

Referencing instance method 'sink(receiveValue:)' on 'Publisher' 
requires the types 'Publishers.Filter<PassthroughSubject<Int, WeatherError>>.Failure' 
(aka 'WeatherError') and 'Never' be equivalent

Surprisingly this code works in the video tutorial. How can I make my WeatherError and Never to be equivalent???

2 Answers

You need to provide both handlers, the completion one, and the value one:

let subscriber = weatherPublisher
    .filter { $0 > 10 }
    .sink(receiveCompletion: { _ in }, receiveValue: { value in
       print("\(value)")
    })

This is needed because the single-argument sink, is available only for publishers that can never fail:

extension Publisher where Self.Failure == Never {
    /// ... many lines of documentation omitted
    public func sink(receiveValue: @escaping ((Self.Output) -> Void)) -> AnyCancellable
}

in Xcode 13 & iOS 15.4 this code needed brackets to compile.

extension Publisher where Self.Failure ==  Never {
    // because the publisher can NEVER FAIL - by design!
    public  func sink(receiveValue: @escaping ((Self.Output) -> Void)) -> AnyCancellable {  }
}
Related