Protocol extension in Swift
Forums:
protocol PCalc {
func addition(a: Int, _ b: Int) -> Int
}
class Calc: PCalc {
func addition(a: Int, _ b: Int) -> Int {
return a + b
}
}
extension PCalc {
func power(a: Int, _ b: Int) -> Int {
return Int(pow(Double(a), Double(b)))
}
}
let calc = Calc()
let value = calc.power(2, 8)
//value is 256
- Log in to post comments