SwiftyEventBus 1.0.2

SwiftyEventBus 1.0.2

maru 维护。



  • Maru-zhang

SwiftyEventBus

CI Status Carthage compatible codecov Version License Platform

SwiftyEventBus 是 iOS 和 Swift 的发布/订阅事件总线。

  • 简化了组件之间的通信
  • 使您的代码简单而优雅
  • 类型安全
  • 更多高级特性:安全事件、粘性事件等...

此外,如果您想阅读文档,请点击此处

使用方法

SwiftyEventBus 非常容易使用,您只需两个步骤

1.注册

您可以在任何地方以任何类型进行注册,它将始终观察直到 EventSubscription 对象被释放。

class DemoViewController: UIViewController {

    var ob: EventSubscription<String>!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        ob = EventBus.`default`.register { (x: String) in
            print(x)
        }
    }
}

2.发布

最后,您只需发布任何实现 EventPresentable 的类型。

EventBus.default.post("Foo")

高级

安全发布

有时你可能会发一条没人注意的消息,这是一种不安全的行为,然后你可以使用这个功能。

EventBus.default.safePost("Foo")

如果没有观察者订阅此类消息,使用 EventBus.default.safePost("Foo") 将会抛出 EventBusPostError.useless 异常,你可以捕获它并进行处理。

/// handle EventBusPostError excetion
do {
    try EventBus.default.safePost("foo")
} catch {
    // do something
}

Rx-扩展

如果你的项目使用 RxSwift,可能需要这个来桥接 SwiftyEventBusRx

pod 'SwiftyEventBus/Rx'

然后,你可以在 RxSwift 生态中使用 SwiftyEventBus🎉

var bag: DisposeBag? = DisposeBag()
EventBus.default.rx.register(String.self)
    .subscribe(onNext: { (x) in
        print(x) /// "foo"
    })
    .disposed(by: bag!)
EventBus.default.post("foo")

示例

要运行示例项目,首先克隆存储库,然后从示例目录运行 pod install

安装

CocoaPods

SwiftyEventBus 通过 CocoaPods 提供。要安装它,只需将以下行添加到 Podfile 中

pod 'SwiftyEventBus'

SwiftyEventBus 同样可从 Carthage 获取,请将其添加到 Cartfile

github "Maru-zhang/SwiftyEventBus"

作者

朱祎,[email protected]

许可证

SwifyEventBus 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。