Episode #126

Swift Classes

11 minutes
Published on July 3, 2014

This video is only available to subscribers. Get access to this video and 584 others.

Continuing on with our Swift exploration, we focus this time on Swift classes. We talk about initializers, inheritance, protocols, type inspection, and more.

Episode Links

Setting up to auto compile with Guard

Add 'guard-shell' to your Gemfile:

source 'https://rubygems.org'
gem 'guard-shell'

Then run bundle install to install it. Then add a Guardfile that specifies what to do with new swift files in the current directory:

# A sample Guardfile
guard :shell do
  watch(/(.*).swift/) do |m| 
    puts
    puts
    puts
    puts "Running #{m[0]}"
    puts `swift -i #{m[0]}`
  end
end

Then run bundle exec guard to start watching for changes.

Class example

protocol Edible {
  var name: String { get }
  var value: Int { get }
}

struct Banana: Edible {
  var name: String { get { return "Banana" } }
  var value: Int { get { return 10 } }
}

struct DogTreat: Edible {
  var name: String { get { return "Dog Treat" } }
  var value: Int { get { return 5 } }
}


class Animal {
  let name: String
  var energy = 100

  init(name: String) {
    self.name = name
  }

  func makeSound() {
    energy -= 5
    printEnergy()
  }

  func eat(food: Edible) {
    println("[\(name) is eating a \(food.name) for \(food.value) energy]")
    energy += food.value
    printEnergy()
  }

  func printEnergy() {
    println("[\(name) now has \(energy) energy]")
  }
}

class Dog: Animal {
  override func makeSound() {
    println("Bark")
    super.makeSound()
  }
}

class Molly: Dog {
  init() {
    super.init(name: "Molly")
  }

  override func eat(food: Edible) {
    if let banana = food as? Banana {
      println("YUCK")
    } else {
      super.eat(food)
    }
  }
}

func play(animal: Animal) {
  println("Playing with \(animal.name)")
  animal.makeSound()
}

let dog = Dog(name: "Fido")
play(dog)
play(dog)
play(dog)
play(dog)

dog.eat(Banana())

Molly().eat(Banana())
Molly().eat(DogTreat())