###下标脚本语法

subscript(index: Int) -> Int {
    get {
      // 返回与入参匹配的Int类型的值
    }

    set(newValue) {
      // 执行赋值操作
    }
}
struct TimesTable {
    let multiplier: Int
    subscript(index: Int) -> Int {
      return multiplier * index
    }
}
let threeTimesTable = TimesTable(multiplier: 3)
println("3的6倍是\(threeTimesTable[6])")
// 输出 "3的6倍是18"

###下标脚本选项

下标脚本允许任意数量的入参索引

struct Matrix {
    let rows: Int, columns: Int
    var grid: Double[]
    init(rows: Int, columns: Int) {
      self.rows = rows
      self.columns = columns
      grid = Array(count: rows * columns, repeatedValue: 0.0)
    }
    func indexIsValidForRow(row: Int, column: Int) -> Bool {
        return row >= 0 && row < rows && column >= 0 && column < columns
    }
    subscript(row: Int, column: Int) -> Double {
        get {
            assert(indexIsValidForRow(row, column: column), "Index out of range")
            return grid[(row * columns) + column]
        }
        set {
            assert(indexIsValidForRow(row, column: column), "Index out of range")
            grid[(row * columns) + columns] = newValue
        }
    }
}

通过闯入 row 和 column 来构造一个 Matrix 实例

var matrix = Matrix(rows: 2, columns: 2)

grid = [0.0, 0.0, 0.0, 0.0]

      		col0  	col1
row0   	  [	0.0,   	0.0,
row1    	0.0,  	0.0	]

将值赋给带有 row 和 column 下标脚本的 matrix 实例表达式可以完成赋值操作

matrix[0, 1] = 1.5
matrix[1, 0] = 3.2

[0.0, 1.5,
 3.2, 0.0]

END