Swift 下标(subscripts译文)

1,490 阅读2分钟

下标是元素访问的一个强大同一的接口,可以覆盖写、重载,可以定义多维下标。

类、结构体、枚举可以定义下标,是集合、列表、序列的成员元素的快捷读写方式,屏蔽各种类型接口的复杂性。 如下面对数组和集合的访问:

someArray[index] someDictionary[key].

可以给一个类型定义多个下标,具体使用那个下标由传入下标 subscript 的索引类型决定。可以定义多维的下标,如 subscript(index: Int, name: String) -> Int 。

You can define multiple subscripts for a single type, and the appropriate subscript overload to use is selected based on the type of index value you pass to the subscript.

下标语法

使用关键字 subscript 开始声明下标,之后是一个或是多个输入参数+一个返回类型,具有读写和只读两种下标。

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

只读下标

放弃setter,省略get关键字,直接填写getter代码:

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"

使用下标

写字典,更多信息请参考 Accessing and Modifying a Dictionary

var numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]numberOfLegs["bird"] = 2

Swift’s Dictionary type implements its key-value subscripting as a subscript that takes and returns an optional type. For the numberOfLegs dictionary above, the key-value subscript takes and returns a value of type Int?, or “optional int”. The Dictionary type uses an optional subscript type to model the fact that not every key will have a value, and to give a way to delete a value for a key by assigning a nil value for that key.

下标选项

下标可以有任意个输入参数,可以是任何类型,可以返回任何的类型。可以接受可变参数列表,并提供默认值 ,请参考 Variadic Parameters and Default Parameter Values,但下标的参数是单向的,只能输入不能输出(函数的参数可以是双向的)。

重载下标的调用是基于值的类型推断,这便是下标重载(subscript overloading)。下面在 Matrix 中实现一个二维下标:

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
        }
    }
}
创建一个实例:
var matrix = Matrix(rows: 2, columns: 2)

../_images/subscriptMatrix01_2x.png

赋值

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

../_images/subscriptMatrix02_2x.png

类型下标

通常意义上的下标是实例下标,作用在实例上;也可定义类型下标,作用在类型上。

enum Planet: Int {
    case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
    static subscript(n: Int) -> Planet {
        return Planet(rawValue: n)!
    }
}
let mars = Planet[4]
print(mars)

对于类,可以使用 class 替代 static,子类可覆写对应的下标。

资料

官方文档

[https://docs.swift.org/swift-book/LanguageGuide/Subscripts.html](https://docs.swift.org/swift-book/LanguageGuide/Subscripts.html)  下标