Sign in to FlowVella

Forgot password?
Sign in with Facebook

New? Create your account

Sign up for FlowVella

Sign up with Facebook

Already have an account? Sign in now


By registering you are agreeing to our
Terms of Service

Share This Flow

Loading Flow

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

init() and deinit() are called when objects are created and destroyed

Objects: initializing and deinitializing

Convenience inits that may return nil must be declared as failable inits

class Workout {

  let exerciser : Person

  let warmup : Exercising

  let primaryExercise : Exercising

  let coolDown : Exercising


  init (warmup : Exercising, primary : Exercising, cooldown : Exercising,

    exerciser : Person) {

    self.warmup = warmup

    self.primaryExercise = primary

    self.coolDown = cooldown

    self.exerciser = exerciser

    println("Stretching your muscles")

    println("Warming up cardio to raise heart rate")

    while exerciser.heartRate < (exerciser.heartRateResting +

      exerciser.heartRateTarget) / 2 {

      exerciser.heartRate += warmup.heartRateIncrement

      println (warmup.describe())

    }

  }


  func Exercise () {

    while exerciser.heartRate < exerciser.heartRateTarget {

      exerciser.heartRate += primaryExercise.heartRateIncrement

      println (primaryExercise.describe())

    }

    println("Target heart rate achieved")

  }


  
deinit {

    println("Cooling down")

    assert(primaryExercise.heartRateIncrement > warmup.heartRateIncrement)

    while exerciser.heartRate > exerciser.heartRateTarget - 20 {

      exerciser.heartRate -= (primaryExercise.heartRateIncrement -

      warmup.heartRateIncrement)

      println (coolDown.describe())

    }

  }

}


var me = Person()

var myWorkout : Workout? = Workout(warmup: Walk(), primary: Jog(),

  cooldown: Walk(), exerciser: me)

myWorkout!.Exercise()


myWorkout = nil

println("myWorkout has ended")

println("My heart rate is now \(me.heartRate)")

https://gist.github.com/mthouser/4f8721f5fb89f5abb28f

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...

Downloading Image /

loading...
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

  • 20

  • 21

  • 22

  • 23

  • 24

  • 25

  • 26

  • 27

  • 28

  • 29

  • 30

Introduction to the Swift Language - Chicago Swift Developer Meetup

By Matt Houser

v 2015-04-13a