AmazingSwiftEvent: C# 事件为 Swift
AmazingSwiftEvent
是一个通用且无保留周期的 事件
,它允许任何类型的实例在发生感兴趣的事情时通知多个类。
使用说明
发送 事件
的类型称为 发布者
,处理 事件
的类称为 订阅者
。
发布者
import AmazingSwiftEvent
protocol CarProtocol {
var started: Event<CarProtocol, Void> { get }
var energyEfficiencyChanged: Event<CarProtocol, (Double, Double)> { get }
func start()
func changeEnergyEfficiency(_ oldValue: Double, _ newValue: Double)
}
class ElectricCar: CarProtocol {
private var startedPublisher: EventPublisher<CarProtocol, Void>
private var energyEfficiencyChangedPublisher: EventPublisher<CarProtocol, (Double, Double)>
var started: Event<CarProtocol, Void>
var energyEfficiencyChanged: Event<CarProtocol, (Double, Double)>
init() {
startedPublisher = EventPublisher()
energyEfficiencyChangedPublisher = EventPublisher()
started = startedPublisher.event
energyEfficiencyChanged = energyEfficiencyChangedPublisher.event
}
func start() {
startedPublisher.invoke(self, ())
}
func changeEnergyEfficiency(_ oldValue: Double, _ newValue: Double) {
energyEfficiencyChangedPublisher.invoke(self, (oldValue, newValue))
}
}
class GasolineCar: CarProtocol {
private var startedPublisher: EventPublisher<CarProtocol, Void>
private var energyEfficiencyChangedPublisher: EventPublisher<CarProtocol, (Double, Double)>
var started: Event<CarProtocol, Void>
var energyEfficiencyChanged: Event<CarProtocol, (Double, Double)>
init() {
startedPublisher = EventPublisher()
energyEfficiencyChangedPublisher = EventPublisher()
started = startedPublisher.event
energyEfficiencyChanged = energyEfficiencyChangedPublisher.event
}
func start() {
startedPublisher.invoke(self, ())
}
func changeEnergyEfficiency(_ oldValue: Double, _ newValue: Double) {
energyEfficiencyChangedPublisher.invoke(self, (oldValue, newValue))
}
}
订阅者
import AmazingSwiftEvent
class CarTestBench {
var energyEfficiencyChangedSubscriber: EventSubscriberProtocol!
init(_ car: CarProtocol) {
// Subscribing to the event using closure expression.
// Event will store self as a weak reference and will pass it to closure expression.
car.started.subscribe(self) { (self: CarTestBench, eventArguments: EventArguments<CarProtocol, Void>) in
// ...
}
// Subscribing to the event using partially-applied function.
// Event will store self as a weak reference and will use it to construct full function.
energyEfficiencyChangedSubscriber = car.energyEfficiencyChanged.subscribe(self, CarTestBench.onEnergyEfficiencyChanged)
}
deinit {
// Optional.
// Event will check if subscriber can handle it and remove it from subscribers if it's not.
energyEfficiencyChangedSubscriber.unsubscribe()
}
func onEnergyEfficiencyChanged(_ eventArguments: EventArguments<CarProtocol, (Double, Double)>) {
if eventArguments.sender is ElectricCar {
// ...
}
else if eventArguments.sender is GasolineCar {
// ...
}
}
}
事件参数
EventArguments
是一个特殊类,将为 订阅者
提供发送者和 事件
的值。
扩展
有 EventPublisher
扩展用于 Void
值类型,以便在没有传递值时简化对 EventPublisher
的使用。此外,您可以通过调用 get()
静态方法同时获取 EventPublisher
和相关联的 Event
。
class ElectricCar: CarProtocol {
// ...
init() {
(startedPublisher, started) = EventPublisher<CarProtocol, Void>.get()
(energyEfficiencyChangedPublisher, energyEfficiencyChanged) = EventPublisher<CarProtocol, (Double, Double)>.get()
}
func start() {
// Equivalent of startedPublisher.invoke(self, ())
startedPublisher.invoke(self)
}
// ...
}
需求
- iOS 11.0+
- Xcode 11+
- Swift 5.0+
安装
CocoaPods
要使用 CocoaPods 在 Xcode 项目中集成 AmazingSwiftEvent
,在 Podfile
中指定它。
pod 'AmazingSwiftEvent', '~> 3.0'
Git 子模块
要使用 Git 子模块将 AmazingSwiftEvent
集成到工作区中,在顶级项目目录中打开 Terminal
并运行以下命令
git submodule add https://github.com/khamitimur/AmazingSwiftEvent.git
如果您尚未将项目初始化为 Git 仓库,请先运行以下命令
git init
您会发现一个新的 AmazingSwiftEvent
文件夹。打开它,将 AmazingSwiftEvent.xcodeproj
拖放到 Xcode 中的项目导航器,或者在您使用 Workspace
的情况下拖放到项目导航器根目录。
选择您的项目,在 General -> Frameworks and Libraries
下添加 AmazingSwiftEvent.framework
。
手动
将 AmazingSwiftEvent.xcodeproj
拖入您的项目在 Xcode
的项目导航器中。或者如果是使用 Workspace
,则拖入项目导航器根目录。
选择您的项目,在 General -> Frameworks and Libraries
下添加 AmazingSwiftEvent.framework
。
协议
此项目采用MIT 许可协议。
致谢
- 灵感来源于Colin Eberhardt的文章。