Is there a way to avoid using AnyPublisher/eraseToAnyPublisher all over the place?

后端 未结 3 1606
太阳男子
太阳男子 2021-02-08 11:46

I\'m just learning how to use Combine. I have experience with Rx (RxSwift and RxJava) and I\'m noticing that it\'s quite similar.

However, one thing that is quite differ

3条回答
  •  我寻月下人不归
    2021-02-08 12:11

    With opaque return the type is defined by what is exactly returned from closure, so you can use just

    func createPublisher() -> some Publisher {
        return Just(1)
    }
    
    let cancellable = createPublisher()
       .print()
       .sink(receiveCompletion: { _ in
           print(">> done")
       }) { value in
           print(">> \(value)")
       }
    
    // ... all other code here
    

    and it works. Tested with Xcode 11.4.

提交回复
热议问题