The subscript script can be defined in the class, structure, and enumeration targets, allowing these types of instances to access attributes and methods quickly through.
Subscript (Index: INT)-> int {get {// return the int type value that matches the input parameter} set (newvalue) {// perform the value assignment operation }}
Subscripts are usually used to access the shortcuts of elements in collection, list, or sequence.
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
= 0 & Column
double {get {assert (indexisvalidforrow (row, column: column), "Index out of range") return grid [(row * columns) + column]} set {assert (indexisvalidforrow (row, column: column), "Index out of range") grid [(row * columns) + column] = newvalue }}var matrix = matrix (rows: 2, columns: 2) Let somevalue = matrix [2, 2]