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
Extensions add behaviors to existing types
Objects: Extensions
//Existing
protocol Exercising {
var heartRateIncrement : Int { get set }
func describe() -> String
}
class Walk : Exercising {
var heartRateIncrement : Int = 10
func describe() -> String { return "walking" }
}
//New
protocol Transporting {
func travelHowManyMiles(timeInMinutes: Float) -> Float
}
extension Walk : Transporting {
func travelHowManyMiles(timeInMinutes: Float) -> Float {
return timeInMinutes * 2.0 / 60.0
}
}
Extensions do not have to be implementations of protocols. Arbitrary functions can be in an extension