【问题标题】:Swift Combine: how to create custom sink?Swift Combine:如何创建自定义接收器?
【发布时间】:2019-10-09 20:46:05
【问题描述】:

我正在使用 RxSwift,如下所示

extension Reactive where Base: UIViewController {

    public var showError: Binder<Error> {
        return Binder(self.base) { element, error in
            let alertVC = // create UIAlertController with error
            element.present(alertVC, animated: true)
        }
    }

用 Swift Combine 构建这样的东西的最佳方法是什么?

【问题讨论】:

    标签: swift rx-swift combine


    【解决方案1】:

    它是内置的。您不需要制作 Bindable... 只需:

    cancelable = publisher.assign(to: \.title, on: barButtonItem)
    

    回答您编辑的问题:

    extension UIViewController {
        var showError: Subscribers.Sink<Error, Never> {
            return Subscribers.Sink<Error, Never>(
                receiveCompletion: { _ in },
                receiveValue: { [unowned self] error in
                    let alertVC = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
                    self.present(alertVC, animated: true)
                }
            )
        }
    }
    

    【讨论】:

    • 好的,这是一个非常糟糕的例子 ? 我编辑了我的帖子 - 我想做一些自定义的事情
    • 新问题,新答案。
    猜你喜欢
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多