Swift 3.0之下标
类,结构体和枚举都可以定义下标,使用下标可以快速访问集合,列表或者序列的数据成员元素。可以使用someArray[index]
来访问Array, 使用someDictionary[key]
来访问Dictionary。
一个类型可以定义多个下标。
定义一个get set的下标:
subscript(index: Int) -> Int {
get {
// return an appropriate subscript value here
}
set(newValue) {
// perform a suitable setting action here
}
}
定义一个read-only的下标
subscript(index: Int) -> Int {
// return an appropriate subscript value here
}
例子:
struct TimesTable {
let multiplier: Int
subscript(index: Int) -> Int {
return multiplier * index
}
}
let threeTimesTable = TimesTable(multiplier: 3)
print("six times three is \(threeTimesTable[6])")
// Prints "six times three is 18"
还可以使用多个下标, 任何类型,除了in-out类型的参数
struct Matrix {
let rows: Int, columns: Int
var grid: [Double]
init(rows: Int, columns: Int) {
self.rows = rows
self.columns = columns
grid = Array(repeating: 0.0, count: rows * columns)
}
func indexIsValid(row: Int, column: Int) -> Bool {
return row >= 0 && row < rows && column >= 0 && column < columns
}
subscript(row: Int, column: Int) -> Double {
get {
assert(indexIsValid(row: row, column: column), "Index out of range")
return grid[(row * columns) + column]
}
set {
assert(indexIsValid(row: row, column: column), "Index out of range")
grid[(row * columns) + column] = newValue
}
}
}