十一、下标 Subscripts

1. 概述

下标 Subscripts 是访问 Collection, List, 和 Sequence 中成员元素的速记方法(Shortcuts)。 Classes, Structures, Enumerations 都可以定义下标 Subscripts 。

2.下标语法 Subscript Syntax

下标的语法与实例的方法(Instance method)语法和计算属性(Computed Properties) 的语法类似。

通过 subscript 关键字定义下标,并且指定一个(或多个)输入参数以及一个返回类型(return type)。

下标可以是 read-write 或  read-only 的,这通过 getter 和 setter 设置,这一点与 Computed Properties 类似。Instance method 没有这种属性。

subscript(index: Int) -> Int {
    get {
        // return an appropriate subscript value here
    }
    set(newValue) {
        // perform a suitable setting action here
    }
}

newValue 的类型与 get方法中返回值的类型相同。与 Computed Properties 一样,当不写set 方法的参数时,会有一个默认的newValue参数。

如果不写set方法,则表明下标subscript是只读的,这时可以省略get关键字

subscript(index: Int) -> Int {
    // return an appropriate subscript value here
}

只读下标的实现:

struct TimeTable {
    let multiplier: Int
    subscript(index: Int) -> Int { //只读下标
        return multiplier * index
    }
}

let threeTimesTable = TimeTable(multiplier: 3);
println("six times three is (threeTimesTable[6])") //six times three is 18

3. Subscript Options

subscript 可以有任意个输入参数。

subscript 的输入参数和返回参数可以是任何类型,但是除开以下类型:

1)in-out 定义的参数

2)有默认值的参数

一个类或结构体可以根据自身需要提供多个subscript的实现,在定义subscript时通过入参个类型进行区分,使用subscript时会自动匹配合适的下标脚本实现运行,这就是subscript的重载

大部分情况下,subscript只有一个传入参数(后面简称入参),但是你也可以根据需要定义多个入参。

例如,定义一个矩阵:

//定义一个矩阵
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: columns), "Index outof range")
            return grid[row * column + column]
        }
        set{
            assert(indexIsValidForRow(row, column: columns), "Index outof range")
            grid[row * column + column] = newValue
        }
    }
}

调用上面创建的矩阵:

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

赋值:

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

如果执行如下操作,那么会触发assert方法:

let someValue = matrix[2, 2]
// this triggers an assert, because [2, 2] is outside of the matrix bounds

参考 https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Subscripts.html#//apple_ref/doc/uid/TP40014097-CH16-ID305

原文地址:https://www.cnblogs.com/actionke/p/4231176.html