Classes, structures, and enumerations can definesubscripts, which are shortcuts for accessing the member elements of a collection, list, or sequence. You use subscripts to set and retrieve values by index without needing separate methods for setting and retrieval. For example, you access elements in anArrayinstance assomeArray[index]and elements in aDictionaryinstance assomeDictionary[key].
下标可以定义在类、结构体和枚举中,是访问集合,列表或序列中元素的快捷方式。可以使用下标的索引,设置和获取值,而不需要再调用对应的存取方法。举例来说,用下标访问一个Array实例中的元素可以写作someArray[index],访问Dictionary实例中的元素可以写作someDictionary[key]。
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. Subscripts are not limited to a single dimension, and you can define subscripts with multiple input parameters to suit your custom type’s needs.
一个类型可以定义多个下标,通过不同索引类型进行重载。下标不限于一维,你可以定义具有多个入参的下标满足自定义类型的需求。
Subscript Syntax (下标语法)
Subscripts enable you to query instances of a type by writing one or more values in square brackets after the instance name. Their syntax is similar to both instance method syntax and computed property syntax. You write subscript definitions with thesubscriptkeyword, and specify one or more input parameters and a return type, in the same way as instance methods. Unlike instance methods, subscripts can be read-write or read-only. This behavior is communicated by a getter and setter in the same way as for computed properties:
下标允许你通过在实例名称后面的方括号中传入一个或者多个索引值来对实例进行存取。语法类似于实例方法语法和计算型属性语法的混合。与定义实例方法类似,定义下标使用subscript关键字,指定一个或多个输入参数和返回类型;与实例方法不同的是,下标可以设定为读写或只读。这种行为由 getter 和 setter 实现,有点类似计算型属性:
subscript(index:Int) ->Int{
get{
// return an appropriate subscript value here
}
set(newValue) {
// perform a suitable setting action here
// 执行适当的赋值操作
}
}
The type ofnewValueis the same as the return value of the subscript. As with computed properties, you can choose not to specify the setter’s(newValue)parameter. A default parameter callednewValueis provided to your setter if you do not provide one yourself.
newValue的类型和下标的返回类型相同。如同计算型属性,可以不指定 setter 的参数(newValue)。如果不指定参数,setter 会提供一个名为newValue的默认参数。
As with read-only computed properties, you can drop thegetkeyword for read-only subscripts:
如同只读计算型属性,可以省略只读下标的get关键字:
subscript(index:Int) ->Int{
// return an appropriate subscript value here
}
Here’s an example of a read-only subscript implementation, which defines aTimesTablestructure to represent ann-times-table of integers:
下面代码演示了只读下标的实现,这里定义了一个TimesTable结构体,用来表示传入整数的乘法表:
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"
In this example, a new instance ofTimesTableis created to represent the three-times-table. This is indicated by passing a value of3to the structure’sinitializeras the value to use for the instance’smultiplierparameter.
在上例中,创建了一个TimesTable实例,用来表示整数3的乘法表。数值3被传递给结构体的构造函数,作为实例成员multiplier的值。
You can query thethreeTimesTableinstance by calling its subscript, as shown in the call tothreeTimesTable[6]. This requests the sixth entry in the three-times-table, which returns a value of18, or3times6.
你可以通过下标访问threeTimesTable实例,例如上面演示的threeTimesTable[6]。这条语句查询了3的乘法表中的第六个元素,返回3的6倍即18。
NOTE
Ann-times-table is based on a fixed mathematical rule. It is not appropriate to setthreeTimesTable[someIndex]to a new value, and so the subscript forTimesTableis defined as a read-only subscript.
TimesTable例子基于一个固定的数学公式,对threeTimesTable[someIndex]进行赋值操作并不合适,因此下标定义为只读的。
Subscript Usage (下标用法)
The exact meaning of “subscript” depends on the context in which it is used. Subscripts are typically used as a shortcut for accessing the member elements in a collection, list, or sequence. You are free to implement subscripts in the most appropriate way for your particular class or structure’s functionality.
下标的确切含义取决于使用场景。下标通常作为访问集合,列表或序列中元素的快捷方式。你可以针对自己特定的类或结构体的功能来自由地以最恰当的方式实现下标。
For example, Swift’sDictionarytype implements a subscript to set and retrieve the values stored in aDictionaryinstance. You can set a value in a dictionary by providing a key of the dictionary’s key type within subscript brackets, and assigning a value of the dictionary’s value type to the subscript:
例如,Swift 的Dictionary类型实现下标用于对其实例中储存的值进行存取操作。为字典设值时,在下标中使用和字典的键类型相同的键,并把一个和字典的值类型相同的值赋给这个下标:
var numberOfLegs= ["spider":8,"ant":6,"cat":4]
numberOfLegs["bird"] =2
The example above defines a variable callednumberOfLegsand initializes it with a dictionary literal containing three key-value pairs. The type of thenumberOfLegsdictionary is inferred to be[String: Int]. After creating the dictionary, this example uses subscript assignment to add aStringkey of"bird"and anIntvalue of2to the dictionary.
上例定义一个名为numberOfLegs的变量,并用一个包含三对键值的字典字面量初始化它。numberOfLegs字典的类型被推断为[String: Int]。字典创建完成后,该例子通过下标将String类型的键bird和Int类型的值2添加到字典中。
For more information aboutDictionarysubscripting, seeAccessing and Modifying a Dictionary.
更多关于Dictionary下标的信息请参考读取和修改字典
NOTE
Swift’sDictionarytype implements its key-value subscripting as a subscript that takes and returns anoptionaltype. For thenumberOfLegsdictionary above, the key-value subscript takes and returns a value of typeInt?, or “optional int”. TheDictionarytype 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 anilvalue for that key.
Swift 的Dictionary类型的下标接受并返回可选类型的值。上例中的numberOfLegs字典通过下标返回的是一个Int?或者说“可选的int”。Dictionary类型之所以如此实现下标,是因为不是每个键都有个对应的值,同时这也提供了一种通过键删除对应值的方式,只需将键对应的值赋值为nil即可。
Subscript Options (下标选项)
Subscripts can take any number of input parameters, and these input parameters can be of any type. Subscripts can also return any type. Subscripts can use variadic parameters, but they can’t use in-out parameters or provide default parameter values.
下标可以接受任意数量的入参,并且这些入参可以是任意类型。下标的返回值也可以是任意类型。下标可以使用变量参数和可变参数,但不能使用输入输出参数,也不能给参数设置默认值。
A class or structure can provide as many subscript implementations as it needs, and the appropriate subscript to be used will be inferred based on the types of the value or values that are contained within the subscript brackets at the point that the subscript is used. This definition of multiple subscripts is known assubscript overloading.
一个类或结构体可以根据自身需要提供多个下标实现,使用下标时将通过入参的数量和类型进行区分,自动匹配合适的下标,这就是下标的重载。
While it is most common for a subscript to take a single parameter, you can also define a subscript with multiple parameters if it is appropriate for your type. The following example defines aMatrixstructure, which represents a two-dimensional matrix ofDoublevalues. TheMatrixstructure’s subscript takes two integer parameters:
虽然接受单一入参的下标是最常见的,但也可以根据情况定义接受多个入参的下标。例如下例定义了一个Matrix结构体,用于表示一个Double类型的二维矩阵。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=0&&column
}
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
}
}
}
Matrixprovides an initializer that takes two parameters calledrowsandcolumns, and creates an array that is large enough to storerows * columnsvalues of typeDouble. Each position in the matrix is given an initial value of0.0. To achieve this, the array’s size, and an initial cell value of0.0, are passed to an array initializer that creates and initializes a new array of the correct size. This initializer is described in more detail inCreating an Array with a Default Value.
Matrix提供了一个接受两个入参的构造方法,入参分别是rows和columns,创建了一个足够容纳rows * columns个Double类型的值的数组。通过传入数组长度和初始值0.0到数组的构造器,将矩阵中每个位置的值初始化为0.0。关于数组的这种构造方法请参考创建一个空数组。
You can construct a newMatrixinstance by passing an appropriate row and column count to its initializer:
你可以通过传入合适的row和column的数量来构造一个新的Matrix实例:
var matrix = Matrix(rows:2, columns:2)
The preceding example creates a newMatrixinstance with two rows and two columns. Thegridarray for thisMatrixinstance is effectively a flattened version of the matrix, as read from top left to bottom right:
上例中创建了一个Matrix实例来表示两行两列的矩阵。该Matrix实例的grid数组按照从左上到右下的阅读顺序将矩阵扁平化存储:
Values in the matrix can be set by passing row and column values into the subscript, separated by a comma:
将row和column的值传入下标来为矩阵设值,下标的入参使用逗号分隔:
matrix[0,1] =1.5
matrix[1,0] =3.2
These two statements call the subscript’s setter to set a value of1.5in the top right position of the matrix (whererowis0andcolumnis1), and3.2in the bottom left position (whererowis1andcolumnis0):
上面两条语句分别调用下标的 setter 将矩阵右上角位置(即row为0、column为1的位置)的值设置为1.5,将矩阵左下角位置(即row为1、column为0的位置)的值设置为3.2:
TheMatrixsubscript’s getter and setter both contain an assertion to check that the subscript’srowandcolumnvalues are valid. To assist with these assertions,Matrixincludes a convenience method calledindexIsValid(row:column:), which checks whether the requestedrowandcolumnare inside the bounds of the matrix:
Matrix下标的 getter 和 setter 中都含有断言,用来检查下标入参row和column的值是否有效。为了方便进行断言,Matrix包含了一个名为indexIsValidForRow(_:column:)的便利方法,用来检查入参row和column的值是否在矩阵范围内:
func indexIsValidForRow(row:Int,column:Int) ->Bool{
return row>=0&&row=0&&column
}
An assertion is triggered if you try to access a subscript that is outside of the matrix bounds:
断言在下标越界时触发:
let someValue = matrix[2,2]
// this triggers an assert, because [2, 2] is outside of the matrix bounds