SwiftEvent 是 iOS 上 Swift 简单的事件机制。
没有参数的事件示例:
var event = Event() event.on() { print("regular listener") } event.once() { print("one time listener") } event.emit() event.emit()
一个参数:
var counter = 0var event = EventWith<Int>() event.on() { counter += $0} event.once() { counter += $0} event.emitWith(1)
两个参数
var counter = 0var string = ''var event = EventDue<Int, String>() event.on() { (value, word) in counter += value string += word } event.emitWith(1, and: 'Hello ') event.emitWith(1, and: 'World')
多个参数
var text = ''var event = EventWith<[String]>() event.on() { (words) in text += " ".join(words) } event.emitWith(['Hello', 'World', '!!!'])