See if the following example code is used:
- var studentlist:string[] = ["Zhang San", "John Doe", "Harry"]
- Studentlist[0] = "Zhuge Liang"
- var studentdictionary = [102: "Zhang San", 105: "John Doe", 109: "Harry"]
- STUDENTDICTIONARY[110] = "Dong Liu"
When accessing arrays and dictionaries, you can use subscript access. Where the subscript of an array is an integer type index, the subscript of the dictionary is its "key".
Subscript
The subscript in Swift is equivalent to indexed properties in Java and indexers in C #.
The syntax format for subscript access is as follows:
- <span style="FONT-FAMILY:COURIER;COLOR:WINDOWTEXT;FONT-SIZE:8PT; Mso-ascii-font-family:courier; Mso-hansi-font-family:courier; mso-bidi-font-family: ' Courier New '; mso-font-kerning:0pt; " ><span style="font-family: Song Body;" > Polygon </span></span> to object Type type name {
- Other properties
- ...
- Subscript (parameter: parameter data type)-> return value data type {
- get {
- return value
- }
- Set (new property value) {
- ...
- }
- }
- }
The subscript also has getter and setter accessors similar to the computed properties .
The getter accessor is a method that returns the result of the calculation using the return statement at the end .
The setter accessor "new property value" is the value to assign to the property. The declaration of the parameter can be omitted, and the system assigns a default parameter, newvalue.
Example: two-dimensional array
There is no two-dimensional array available in Swift, only one-dimensional array . You can customize a two-dimensional array type, and then access its elements through two subscript parameters, formally similar to the two-dimensional array of C language.
The example code for a two-dimensional array with subscript is as follows:
- struct Doubledimensionalarray {//defines a two-dimensional array structure body
- Let Rows:int, Columns:int//Store properties rows and columns
- var grid: [Int]
- Init (Rows:int, Columns:int) {//constructor
- self.rows = rows
- self.columns = columns
- Grid = Array (count:rows * columns, repeatedvalue:0)//Initialize storage Properties grid
- }
- Subscript (Row:int, Col:int)-> Int {//define subscript
- get {
- return grid[(Row * columns) + col]
- }
- Set (newValue1) {
- grid[(Row * columns) + col] = newValue1
- }
- }
- }
- var ary2 = Doubledimensionalarray (rows:10, columns:10)//Create and initialize a two-dimensional array of 10x10 size
- Initialize a two-dimensional array
- for var i = 0; I < ten; i++ {
- for var j = 0; J <
- ARY2[I,J] = i * j
- }
- }
- Print out a two-dimensional array
- for var i = 0; I < ten; i++ {
- for var j = 0; J <
- Print ("\ t \ (Ary2[i,j])")
- }
- Print ("\ n")
- }
The output results are as follows:
0 0 0 0 < Span lang= "en-US" > 0 0 0 0 0 0
0 1 2 3 < Span lang= "en-US" > 4 5 6 7 8 9
0 2 4 6 < Span lang= "en-US" > 8 10 12 14 16 18
0 3 6 9 < Span lang= "en-US" > 12 15 18 21 24 27
0 4 8 12 < Span lang= "en-US" > 16 20 24 28 32 36
0 5 10 15 20 25 30 35 40 45
0 6 12 18 24 30 36 42 48 54
0 7 14 21 28 35 42 49 56 63
0 8 16 24 32 40 48 56 64 72
0 9 81
swift-Subscript-Prepare