BjornRuud / Event

An implementation of the observer pattern in Swift.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Swift Events

Swift currently lacks an observation mechanism like KVO. KVO can still be used if you make sure your classes use the Objective-C runtime, but that's not very "swifty". This project is an implementation of the observer pattern, which can also replace NotificationCenter for general event messaging in many use cases.

Event

Events are defined by the class Event<T>. The generic type is the type of the data you want to publish for this event. You then subscribe to the event and provide a handler for doing something with the data.

Here is an example class that publishes an event when a property is set:

class Foo {
    let valueChanged = Event<(oldValue: Int, newValue: Int)>()

    var value = 0 {
        didSet {
            valueChanged.publish((oldValue: oldValue, newValue: value))
        }
    }
}

class Bar {}

let foo = Foo()
let bar = Bar()

foo.valueChanged.subscribe(bar) {
    oldValue, newValue in
    print("Value changed from \(oldValue) to \(newValue)")
}
foo.value = 42

Property

The Property class uses Event to provide a convenient way to observe value changes. You can create it as a property and then use the value property on the Property to make value changes.

class Foo {
    let bar = Property<Int>(0)
}

let foo = Foo()
let ob = SomeClass()
foo.bar.add(observer: ob) {
    oldValue, newValue in
    // Do stuff
}
foo.bar.value = 42

About

An implementation of the observer pattern in Swift.


Languages

Language:Swift 94.8%Language:Objective-C 5.2%