Swift Examples - Subscripts

The Swift Programming Language Examples

源码在 GitHub:https://github.com/gewill/The-Swift-Programming-Language-2.1-Examples

写了一段时间的 Swift 后,又来恶补基础知识了。

Playground ->

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// : Playground - noun: a place where people can play

import UIKit

//: 下标(Subscripts)

//: 下标允许你通过在实例名称后面的方括号中传入一个或者多个索引值来对实例进行存取。

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 {
precondition(indexIsValidForRow(row, column: column), "Index out of range")
return grid[(row * columns) + column]
}

set {
precondition(indexIsValidForRow(row, column: column), "Index out of range")
grid[(row * columns) + column] = newValue
}
}
}

var matrix = Matrix(rows: 2, columns: 3)
print(matrix[1, 2])
matrix[1, 1] = 3
print(matrix)