MagicSwiftBus 3.1.0

MagicSwiftBus 3.1.0

favret 维护。



  • favre

codebeat badge

MagicSwiftBus

使用具有 EventBus 风格的 NotificationCenter

MagicSwiftBus 是什么?

  • 实现对象间的发布-订阅通信。
  • 避免因缺少字符而崩溃,使用枚举代替。
  • 不要将 NSNotification 用作您方法参数,直接定义实际参数。
  • 在一个协议中定义所有通信。

灵感来自

在 MagicSwiftBus 之前

注册 / 注销

NSNotificationCenter.defaultCenter().addObserver(
    self,
    selector: #selector(testSuccess(_:)),
    name: "MyNotificationTest",
    object: nil)

发布通知

NSNotificationCenter.defaultCenter().postNotificationName("MyNotificationTest", object: "bonjour" as AnyObject)

实现通知方法

func testSuccess(notification: Notification) {
  if let object = notification.object as String {
  }
}

使用 MagicSwiftBus

注册/注销

Bus.register(self, event: .Test, with: "bonjour")

发布通知

Bus.post(.Test)

实现通知方法

func testSuccess(str: String) {
  print(str)
}

安装

CocoaPods

CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它

$ gem install cocoapods

要使用 CocoaPods 将 MagicSwiftBus 集成到您的 Xcode 项目中,请在您的 Podfile 中执行

platform :ios, '9.0'
use_frameworks!

target '<Your Target Name>' do
  pod 'MagicSwiftBus'
end

然后,运行以下命令

$ pod install

Carthage

Carthage 是一种去中心化的依赖管理器,它构建你的依赖并提供二进制框架。

你可以使用以下命令通过 Homebrew 来安装 Carthage

$ brew update
$ brew install carthage

要使用 Carthage 将 Magic-Swift-Bus 集成到你的 Xcode 项目中,请在你的 Cartfile 中指定它

github "favret/Magic-Swift-Bus" => 3.0.0

运行 carthage update 来构建框架,并将构建的 MagicSwiftBus.framework 拖到你的 Xcode 项目中。

手动操作

如果你不想使用上述任一依赖管理器,你可以手动将 Magic-Swift-Bus 集成到你的项目中作为嵌入式框架。

使用方法

创建你的事件协议

@objc protocol MyEvent {
  func testSuccess(str:String)
  func makeCoffee()
}

在这里,你可以看到参数是一个 String,但它可以是其他任何东西。

实现你的事件协议

extension ViewController: MyEvent {

  func makeCoffee() {
    print("☕️")
  }

  func testSuccess(str: String) {
    print(str)
  }
}

在这个例子中,MyReceiver可以是UIViewController或者NSObject或者你想让它成为的任何类型。

将你的事件协议添加到 MagicSwiftBus

class MyBus: Bus {
  enum EventBus: String, EventBusType {
    case test, makeCoffee

    var notification: Selector {
      switch self{
      case .test: return #selector(MyEvent.testSuccess(str:))
      case .makeCoffee: return #selector(MyEvent.makeCoffee)
      }
    }
  }
}

首先,你需要创建一个继承自 Bus 的类。然后,实现 EventBusType 协议。在上述例子中,这个协议是通过一个枚举实现的。

不要忘记注册和注销

MyBus.register(observer: self, events: .test, .makeCoffee)
...
MyBus.unregisterAll(observer: self)

发出简单通知

最后,您可以像这样触发一个事件

    MyBus.post(event: .test, with: "hello world")

线程

在主线程上发布事件

MyBus.postOnMainThread(event: .test, with: "hello world")

在后台线程(默认)发布事件

MyBus.postOnBackgroundThread(event: .test, with: "hello world")

在特定队列上发布事件

MyBus.post(on: DispatchQueue.global(qos: .background), event: .test, with: "hello world")

示例

import UIKit
import MagicSwiftBus

@objc protocol MyEvent {
  func testSuccess(str:String)
  func makeCoffee()
}

class MyBus: Bus {
  enum EventBus: String, EventBusType {
    case test, makeCoffee

    var notification: Selector {
      switch self{
      case .test: return #selector(MyEvent.testSuccess(str:))
      case .makeCoffee: return #selector(MyEvent.makeCoffee)
      }
    }
  }
}

class ViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }

  // Register / Unregister
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    MyBus.register(observer: self, events: .test, .makeCoffee)
    MyBus.post(event: .test, with: "helloWorld")
    MyBus.post(event: .makeCoffee)
  }

  override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    MyBus.unregisterAll(observer: self)
  }
}

//Implement method that you want to use
extension ViewController: MyEvent {

  func makeCoffee() {
    print("☕️")
  }

  func testSuccess(str: String) {
    print(str)
  }
}