Swift Functional Programming(Second Edition)
上QQ阅读APP看书,第一时间看更新

Subscripts

Subscripts are shortcuts to access the member elements of a collection, list, sequence, or any custom type that implement subscripts. Consider the following example:

struct TimesTable { 
let multiplier: Int
subscript(index: Int) ->Int {
return multiplier * index
}
}

let fiveTimesTable = TimesTable(multiplier: 5)
print("six times five is \(fiveTimesTable[6])")
// prints "six times five is 30"