The Swift Brown Fox Says …

I wanted a way to include several topics in one post for beginner Swifters, so here you go:

  • A silly meme
  • A simple protocol
  • Multiple simple implementations of a protocol
  • Pseudo-random number generation within a range
  • Null (Nil) Object Pattern
  • Make a string lowercase

I’ll be referencing a meme/song, “What Does the Fox Say?“, because I couldn’t think of anything better.

A simple protocol that requires all Animals to “say” something:

public protocol Animal {
    func say() -> String
}

 

Some simple protocol implementations for common animals:

public class Dog: Animal {
    public func say() -> String { return "woof" }
}

public class Cat: Animal {
    public func say() -> String { return "meow" }
}

public class Bird: Animal {
    public func say() -> String { return "tweet" }
}

public class Mouse: Animal {
    public func say() -> String { return "squeak" }
}

public class Cow: Animal {
    public func say() -> String { return "moo" }
}

public class Frog: Animal {
    public func say() -> String { return "croak" }
}

public class Elephant: Animal {
    public func say() -> String { return "toot" }
}

public class Duck: Animal {
    public func say() -> String { return "quack" }
}

public class Fish: Animal {
    public func say() -> String { return "blub" }
}

public class Seal: Animal {
    public func say() -> String { return "ow ow ow" }
}

 

Randomly selecting from a list of “sayings” from an unusual animal:

public class Fox: Animal {
    private let phrases = [ "Ring-ding-ding-ding-dingeringeding!",
                            "Gering-ding-ding-ding-dingeringeding!",
                            "Wa-pa-pa-pa-pa-pa-pow!",
                            "Hatee-hatee-hatee-ho!",
                            "Joff-tchoff-tchoff-tchoffo-tchoffo-tchoff!",
                            "Jacha-chacha-chacha-chow!",
                            "Fraka-kaka-kaka-kaka-kow!",
                            "A-hee-ahee ha-hee!",
                            "A-oo-oo-oo-ooo!",
                            "Woo-oo-oo-ooo!"]
    
    public func say() -> String {
        let numberOfPhrases = UInt32(phrases.count)
        let index = Int(arc4random_uniform(numberOfPhrases))
        return phrases[index]
    }
}

 

One of the things I really think I like about Swift is that if you don’t declare a variable as optional (using the ?), it is guaranteed to have a value. You can leverage the Null(Nil) Object Pattern for unknown animals so that you never have to check for nil:

public class NullAnimal: Animal {
    public func say() -> String { return "..." }
}

 

Converting a string to lowercase for more robust matching:

func whatDoesThe(name: String) -> Animal {
    switch(name.lowercaseString){
        case "dog": return Dog()
        case "cat": return Cat()
        case "bird": return Bird()
        case "mouse": return Mouse()
        case "cow": return Cow()
        case "frog": return Frog()
        case "elephant": return Elephant()
        case "duck": return Duck()
        case "fish": return Fish()
        case "seal": return Seal()
        case "fox": return Fox()
        default: return NullAnimal()
    }
}

 

Playing with our new toy:

whatDoesThe("dog").say() // "woof"
whatDoesThe("fox").say() // randomly selected phrase
whatDoesThe("liger").say() // "..."
Share