Painted levels: being fostered fostered fostered

Tags: “iOS” “Swift 5.1” “subscript” author: Mu Ling Luo Review: QiShare team


The subscript

Subscripts are a shortcut for accessing elements in a collection, list, or sequence. When we get or set the value of a member element of a type, we can use subscripts to set and retrieve the corresponding element value through the index, without requiring a separate method.

The subscript grammar

The subscript keyword represents the definition of subscript and specifies the parameters and return types required for subscript method calls after subscript. The same way instance methods are defined. But subscripts support read/write or read-only of instance properties, which requires getters and setters just like computed properties. Subscripts allow us to query and set operations by writing one or more parameter values in square brackets after a type instance. Declaration of reading and writing subscripts:

Subscript (index: Int)->Int{get {/* return value of integer type */} set {/* Set operation */}}Copy the code

Read-only subscript declaration:

Subscript (index: Int)->Int{get {/* return integer type of value */}}Copy the code

The declaration of read-only subscripts, like read-only computed properties, can be simplified by removing the get keyword and its curly braces:

Subscript (index: Int)->Int{/* return integer type */}Copy the code

The subscript use

With respect to the use of subscripts, in most cases the subscript form of a single parameter is the most common. However, in appropriate scenarios, subscripts can take any number of input parameters of any type, and can also return instances of any type as the return value. Subscript arguments can use mutable arguments that provide default values, but not inout. Subscript overloading: Classes or structures can provide multiple implementations of subscripts (with consistent names, inconsistent arguments, or inconsistent return values) as needed. The form in which multiple subscript implementations are defined in the same class or structure is called subscript overloading. Use example: define a two-dimensional Matrix Matrix structure, and achieve the use of subscript Matrix can be according to the row, column to obtain and set the value of the Matrix.

struct Matrix { var description: String{ var line = 0 var desStr = "" for item in matrixArray.enumerated() { if item.0 / columns == line { desStr += "|" + String (item 1) + ""} else {line = item. 0 / columns desStr + =" | "/ / a newline desStr + =" \ n "+" | "+ String (item. 1)}} Return "the output of the matrix of:" + "\ n" + desStr + "|"} let rows: Int, the columns: Int var matrixArray: [Double] init (rows: Int, the columns: Int) {self.rows = rows self.columns = columns matrixArray = array.init (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: Return matrixArray[row * columns + columns]} set {assert(indexIsValid(row: row, column: MatrixArray [row * columns + column] = newValue}} //! Overloading ` Matrix ` subscript: define the accumulation coefficient set subscript (row: Int, column: Int, factor: Double) - > Double {get {assert (indexIsValid (row: Return matrixArray[row * columns + columns]} set {assert(indexIsValid(row: MatrixArray [row * columns + column] = newValue * factor}}Copy the code

Call method with result print

var matrix = Matrix.init(rows: 3, columns: 3) print (matrix. The description) / * output matrix of: | | | | 0.0 0.0 0.0 0.0 0.0 | | | 0.0 | | | | | * 0.0 0.0 0.0 / matrix [2] = 3.8 / /! Print (matrix. Description) /* print(matrix. Description) /* print(matrix. Description) /* print(matrix. | | 0.0 0.0 0.0 | | | | | | 0.0 0.0 0.0 0.0 0.0 | | | | * 3.8 / matrix [1, 1] = 6.0 / /! Print (matrix. Description) /* print(matrix. Description) /* | 0.0 0.0 0.0 | | | | | | | 0.0 6.0 0.0 0.0 0.0 | | | | * 3.8 / matrix,0,2.0 [0] = 4.5 print (matrix. The description) / * of the output matrix of the: | | | | 0.0 0.0 9.0 0.0 6.0 | | | 0.0 | | | | | 3.8 * 0.0/0.0Copy the code

Type the subscript

Instance subscripts are subscripts called on instances of a particular type. We can also define type subscripts. Type subscripts: subscripts defined on the type itself. Method: Indicate type subscripts by writing the static keyword before the subscript keyword. Classes can use the class keyword to allow subclasses to override the implementation of that subscript in their parent class.

Resources: Swift 5.1 official programming guide


Recommended articles:

Today we are going to talk about WebSocket (iOS/Golang) using Swift for Bessel curve drawing Swift 5.1 (11) – Method Swift 5.1 (10) – Properties iOS App background keep live Strange dance weekly