Sign up for FlowVella
Sign up with FacebookAlready have an account? Sign in now
By registering you are agreeing to our
Terms of Service
Loading Flow
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