Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
An eventing mechanism for Swift
/// An object that has some tear-down logic
public protocol Disposable {
func dispose()
/// An event provides a mechanism for raising notifications, together with some
/// associated data. Multiple function handlers can be added, with each being invoked,
/// with the event data, when the event is raised.
public class Event<T> {
public typealias EventHandler = T -> ()
private var eventHandlers = [Invocable]()
public init() {
/// Raises the event, invoking all handlers
public func raise(data: T) {
for handler in self.eventHandlers {
/// Adds the given handler
public func addHandler<U: AnyObject>(target: U, handler: (U) -> EventHandler) -> Disposable {
let wrapper = EventHandlerWrapper(target: target, handler: handler, event: self)
return wrapper
// MARK:- Private
// A protocol for a type that can be invoked
private protocol Invocable: class {
func invoke(data: Any)
// takes a reference to a handler, as a class method, allowing
// a weak reference to the owning type.
// see:
private class EventHandlerWrapper<T: AnyObject, U> : Invocable, Disposable {
weak var target: T?
let handler: T -> U -> ()
let event: Event<U>
init(target: T?, handler: T -> U -> (), event: Event<U>){ = target
self.handler = handler
self.event = event;
func invoke(data: Any) -> () {
if let t = target {
handler(t)(data as U)
func dispose() {
event.eventHandlers = event.eventHandlers.filter { $0 !== self }
Copy link

klaas commented May 24, 2015

How about automatically disposing the EventHandlerWrapper when the target reference is nil within the invoke method?

Copy link

galblank commented Mar 4, 2016

it seems that this method only works on simple types such as int / string etc..
how about Arrays and Dictionaries?

Copy link

nhatlee commented Sep 9, 2016

It is great to start writing KVO. But how to remove the observer?

Copy link

Jcarto commented Nov 21, 2016

I am getting a build error on the eventHandlers.
in the dispose function it is saying that eventHandlers is inaccessible due to the private protection level, but you can't remove private because of the [Invocables] ...

Ideas how to fix?

Copy link

Vladlex commented Nov 25, 2016

Make if file private:
fileprivate var eventHandlers = [Invocable]()

Ideas how to fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment