Skip to content
Branch: master
Go to file

Latest commit


Failed to load latest commit information.
Latest commit message
Commit time

Swift Custom Events

A very simple way to implement Backbone.js style custom event listeners and triggering in Swift for iOS development.

This provides a convenient way to organize your code. It's especially useful for game systems like keeping track of events that count towards achievements.

Updated for Swift 3.


Custom event listeners allow us to separate our concerns and prevent function call spaghetti. We can keep the event agnostic while we add and remove functionality.

I prefer creating an 'Events' property of type EventManager on each Class you wish to track. Alternatively, you can also inherit this code onto any classes you like. Or, you can maintain one EventsManager object for your entire app (but listener performance may suffer with many listeners).

This system is a bit more simple and works differently than Backbone's (for one, they use JS prototypes to get their Event code onto all backbone objects). It's intended as a starting place for custom events in Swift.


Let's create a cat class that can bug us while we're coding:

class Cat {
    let events = EventManager();
    func meow() {

And a human class to represent ourselves:

class Human {
    func adoptCat(cat:Cat) {
        // you can pass in an anonymous code block to the event listener"meow", action: {
            println("Human: Awww, what a cute kitty *pets cat*");
        // or you can pass a function reference"meow", action: self.dayDream);
        // Using the information from the trigger:
        // (notice the parameters for ponderCat)"meow", action: self.ponderCat);
    func dayDream() {
        println("Human daydreams about owning a dog");
    func ponderCat(information:Any?) {
        if let info = information as? String {
            println("Oooh, I think I know:");

Play out our little scene:

let zeus = Cat();
let stephen = Human();

 * Cat: MRaawwweeee
 * Human: Awww, what a cute kitty *pets cat*
 * Human daydreams about owning a dog
 * Oooh, I think I know:
 * The cat is hungry!


This is potentially slower than using delegates or direct function calls (see performance testing, below). On the other hand, it's easier and faster to implement (especially in cases with multiple listeners per event).

This is a very early system developed for my specific needs. Feel free to use and adapt as you need, and please send any feedback or improvements.

Performance Testing:

I performed a real world comparison between using event triggers and a direct function call. On average, the event trigger took 0.23 milliseconds to get into the action code. Direct function calls performed in ~0.097 milliseconds. Phew, that's quick! More testing is needed, but I expected much worse results. Function calls are twice as fast, but event triggers using this code are still pretty quick!

Potential improvements/considerations:

  • I haven't tested what happens if you remove instances of either the listener or event object from memory after wiring up a listener.
  • Needs performance testing and tuning.
  • Ability to remove only a specific action from listener array.






No releases published

Contributors 4



You can’t perform that action at this time.