Swift 2.0 Study Note (Day 35)-Will the subscript be used?

Source: Internet
Author: User

Original articles, welcome reprint. Reprint Please specify: Dongsheng's Blog

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

Swift the subscript in is equivalent to Java the index properties in and C # the indexer in the.

The syntax format for subscript access is as follows:

Object Type type name {other properties ... subscript (parameter: parameter data type)-return value data type {get {return return value}              Set (new property value) {...} }            }

The subscript also has a property similar to the computed Getter and the Setter accessor.

Getter accessor is a method that is used at the end of the return The statement returns the result of the calculation.

Setter The 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

in the Swift no two-dimensional arrays are provided, only one-dimensional arrays 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         let Rows:int, Columns:int       //Storage properties rows and columns    var grid: [int]         Init (rows:int, Columns:int) {  //constructors         self.rows = rows& nbsp;       self.columns = columns        Grid = Array (count:rows * columns, repeatedvalue:0)  //Initialize storage properties grid   }      & nbsp 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 < 10; i++ {    for var j = 0, J <, J + + {        Ary2[i,j] = i * j              }} //print output two-dimensional array for var i = 0; I < 10; i++ {    for var j = 0; J <, J + + {        print ("\ t \ (Ary2[i,j])")    }    Print ("\ n")}

The output results are as follows:

0   0  0  0  0  0

0 Span style= "COLOR:WINDOWTEXT; Font-family:courier; font-size:11px; " > 1  3  5  7  9

0 Span style= "COLOR:WINDOWTEXT; Font-family:courier; font-size:11px; " > 2  6  10  14  18

0 Span style= "COLOR:WINDOWTEXT; Font-family:courier; font-size:11px; " > 3  9  15  21  27

0 Span style= "COLOR:WINDOWTEXT; Font-family:courier; font-size:11px; " > 4  12  20  28  36

0 Span style= "COLOR:WINDOWTEXT; Font-family:courier; font-size:11px; " > 5  15  25  35  45

0 Span style= "COLOR:WINDOWTEXT; Font-family:courier; font-size:11px; " > 6  18  30  42  54

0 Span style= "COLOR:WINDOWTEXT; Font-family:courier; font-size:11px; " > 7  21  35  49  63

0 Span style= "COLOR:WINDOWTEXT; Font-family:courier; font-size:11px; " > 8  24  40  56  72

0 Span style= "COLOR:WINDOWTEXT; Font-family:courier; font-size:11px; " > 9  27  45  63  81


Welcome to follow Dongsheng Sina Weibo @tony_ Dongsheng.

Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom

for more iOS,Cocos, mobile design courses, please follow our official website: http://www.zhijieketang.com
Smart-Jie Classroom Forum website: http://51work6.com/forum.php

Swift 2.0 Study Note (Day 35)-Will the subscript be used?

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.