# Swift下標

#### 下標宣告語法及其用法

``````subscript(index: Int) ?> Int {
get {
// used for subscript value declarations
}
set(newValue) {
// definitions are written here
}
}
``````

``````struct subexample {
let decrementer: Int
subscript(index: Int) -> Int {
return decrementer / index
}
}
let division = subexample(decrementer: 100)

print("The number is divisible by \(division[9]) times")
print("The number is divisible by \(division[2]) times")
print("The number is divisible by \(division[3]) times")
print("The number is divisible by \(division[5]) times")
print("The number is divisible by \(division[7]) times")
``````

``````The number is divisible by 11 times
The number is divisible by 50 times
The number is divisible by 33 times
The number is divisible by 20 times
The number is divisible by 14 times
``````

``````class daysofaweek {
private var days = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "saturday"]
subscript(index: Int) -> String {
get {
return days[index]
}
set(newValue) {
self.days[index] = newValue
}
}
}
var p = daysofaweek()

print(p[0])
print(p[1])
print(p[2])
print(p[3])
``````

``````Sunday
Monday
Tuesday
Wednesday
``````

#### 下標中的選項

``````struct Matrix {
let rows: Int, columns: Int
var print: [Double]
init(rows: Int, columns: Int) {
self.rows = rows
self.columns = columns
print = Array(count: rows * columns, repeatedValue: 0.0)
}
subscript(row: Int, column: Int) -> Double {
get {
return print[(row * columns) + column]
}
set {
print[(row * columns) + column] = newValue
}
}
}
var mat = Matrix(rows: 3, columns: 3)

mat[0,0] = 1.0
mat[0,1] = 2.0
mat[1,0] = 3.0
mat[1,1] = 5.0

print("\(mat[0,0])")
``````

``````1.0
``````

Swift 4下標支援單個引數到適當資料型別的多個引數宣告。 程式將`Matrix`結構宣告為`2 * 2`維陣列矩陣，以儲存`Double`資料型別。 `Matrix`引數輸入整數資料型別，用於宣告行和列。

``````var mat = Matrix(rows: 3, columns: 3)
``````

``````mat[0,0] = 1.0
mat[0,1] = 2.0
mat[1,0] = 3.0
mat[1,1] = 5.0
``````