1. Overview
Subscript subscripts is a shorthand method for accessing member elements in Collection, list, and Sequence (shortcuts). Classes, structures, enumerations can all define subscript subscripts.
2. Under the banner law subscript Syntax
The syntax of the subscript is similar to the syntax of the instance method (Instance) syntax and the computed property (Computed properties).
The subscript is defined by the subscript keyword, and you specify one (or more) input parameters and a return type (return).
The subscript can be read-write or read-only, which is set by getter and setter, which is similar to Computed Properties. Instance method does not have this attribute.
Subscript (indexint), int { get { // return an Appropriate subscript valuehere } set(newvalue) { // perform a suitable setting actionhere }}
The type of newvalue is the same as the type of the return value in the Get method. As with Computed Properties, there is a default newvalue parameter when the parameters of the set method are not written.
If you do not write the set method, the subscript subscript is read-only, and you can omit the Get keyword
Subscript (index:int), Int { // return an appropriate subscript valuehere}
Read-only implementation of the subscript:
struct Timetable {Let multiplier:int subscriptintint// Read-only subscript return multiplier * index }}let3); println ("six times three is \ (Threetimestable[6])"//six times three is
3. Subscript Options
Subscript can have any of the input parameters.
The input and return parameters of the subscript can be of any type, except for the following types:
1) parameters defined by In-out
2) Parameters with default values
A class or struct can provide multiple implementations of subscript according to its own needs, which is differentiated by the type of the subscript when defining the subscript, and automatically matches the appropriate subscript script to run when used, which is the subscript overload .
In most cases, subscript has only one incoming parameter (hereafter referred to as a parameter), but you can also define multiple input parameters as needed.
For example, define a matrix:
//Define a matrixstructmatrix{ let rows: int, columns: int vargrid: [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{returnRow >=0&& row < rows && column >=0&& Column <Columns} Subscript (row: int, column: int)- Double{Get{ assert(Indexisvalidforrow (Row, column:columns),"Index outof Range") returnGrid[row * column +Column]} Set{ assert(Indexisvalidforrow (Row, column:columns),"Index outof Range") Grid[row* column + column] =newvalue} }}
Call the matrix created above:
var 2 2)
Assignment value:
matrix[011.5matrix[103.2
The Assert method is triggered if you do the following:
Let somevalue = matrix[22]// This triggers an assert, because [2, 2] is outside of the matrix bounds
Reference Https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language /subscripts.html#//apple_ref/doc/uid/tp40014097-ch16-id305
Xi. subscript subscripts