Joyful Games – iPhone game development with Apple’s Swift new language

Email Contact

Play SnakeJoy! The most addicting fun snake game on the internet

Play SnakeJoy in Your Browser!

Event Triggers and Event Listeners in Swift, Updated Swift 6.2

I recently updated my mini-library that adds Backbone.js style listeners and triggers to Swift. I improved the ability to pass information between triggers and listeners, and updated the syntax for Swift 6.2, using Xcode 6.3 beta 2. Using custom events is a pretty nifty way of organizing your Swift project.


Download it here: Swift Custom Events on GitHub


Here’s an example of how Swift Custom Events works:

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

class Cat {
    let events = EventManager();

    func meow() {
        println("Cat: MRaawwweeee");"meow", information: "The cat is hungry!");

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!

Try it out and let me know how it goes: Swift Custom Events on GitHub