swift-Subscript-Prepare

Source: Internet
Author: User

See if the following example code is used:

 
    1. var studentlist:string[] = ["Zhang San", "John Doe", "Harry"]
    2. Studentlist[0] = "Zhuge Liang"
    3. var studentdictionary = [102: "Zhang San", 105: "John Doe", 109: "Harry"]
    4. 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:

 
  1. <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 {
  2. Other properties
  3. ...
  4. Subscript (parameter: parameter data type)-> return value data type {
  5. get {
  6. return value
  7. }
  8. Set (new property value) {
  9. ...
  10. }
  11. }
  12. }

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:

 
  1. struct Doubledimensionalarray {//defines a two-dimensional array structure body
  2. Let Rows:int, Columns:int//Store properties rows and columns
  3. var grid: [Int]
  4. Init (Rows:int, Columns:int) {//constructor
  5. self.rows = rows
  6. self.columns = columns
  7. Grid = Array (count:rows * columns, repeatedvalue:0)//Initialize storage Properties grid
  8. }
  9. Subscript (Row:int, Col:int)-> Int {//define subscript
  10. get {
  11. return grid[(Row * columns) + col]
  12. }
  13. Set (newValue1) {
  14. grid[(Row * columns) + col] = newValue1
  15. }
  16. }
  17. }
  18. var ary2 = Doubledimensionalarray (rows:10, columns:10)//Create and initialize a two-dimensional array of 10x10 size
  19. Initialize a two-dimensional array
  20. for var i = 0; I < ten; i++ {
  21. for var j = 0; J <
  22. ARY2[I,J] = i * j
  23. }
  24. }
  25. Print out a two-dimensional array
  26. for var i = 0; I < ten; i++ {
  27. for var j = 0; J <
  28. Print ("\ t \ (Ary2[i,j])")
  29. }
  30. Print ("\ n")
  31. }


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.