Swift中的subscript

Swift中通过subscript来定义角标
其中 类、结构体和枚举都可以定义subscripts,
就像数组一样使用一个实例

如下面的代码例子
struct TimesTable {
    let multiplier: Int
    subscript(index: Int) -> Int {
        return multiplier * index
    }
}
let threeTimesTable = TimesTable(multiplier: 3)
println("six times three is \(threeTimesTable[6])")
// prints "six times three is 18

猜你喜欢

转载自liyunpeng.iteye.com/blog/2078809