Protocol subscript requirements in Swift
Forums:
protocol IntIndex {
subscript(i: Int) -> Int {get}
}
class PowerOfTwo : IntIndex {
subscript(i: Int) -> Int {
return Int(pow(Double(2), Double(i)))
}
}
let power = PowerOfTwo()
let p8 = power[8]
//p8 is 256
let p16 = power[16]
//p16 is 65536
- Log in to post comments