Swift note (12) -- subscript

Source: Internet
Author: User

Subscript swift allows us to define Subscripts for classes, struct, and enumeration to access a large number of attributes in a more convenient way. For example, both Array and Dictionary are struct, and swift engineers have provided code for subscript operations for these two types. Therefore, we can use myArray [2] in this way, read and Write the data stored in the struct. In addition, multiple subscript access methods can be defined in a type (reload: For reload, we will mention in subsequent notes that you don't need to care too much about it here)
Subscript can be defined as "read/write" type or "read-only" type. This behavior is completed by defining a group of operations, this is the same as the setter and getter attributes used for calculation mentioned in previous notes:
Subscript definition form:
"Read/write" type: subscript (index: Int)-> Int {get {
} Set (newValue ){
}}
"Read-only" type: subscript (index: Int)-> Int {// As mentioned in previous notes, only one get method can save the get {And .}
Official example:

A struct called TimesTable is defined. It has a "read-only" type submark definition. Through the subscript syntax, the index value of multiplier can be obtained as many times as that of multiplier... Because that's multiplier * index .... Therefore, the result of the last println is 18. (The multiplier value is initialized to 3, 3*6 = 18)
Subscript can use any type as the input parameter. That is to say, we can use class, struct, enum, Int, Double, String, and so on as the underlying parameter, however, this parameter cannot be in-out or have a default value.
I have not mentioned any words about "multi-dimensional array" in the previous notes, and there are no examples, because I don't know how to get it, attention. However, with a custom subscript, things become simple, and everything is up to us, whether it is two-dimensional, three-dimensional, four-dimensional, what type to store in any dimension, etc, everything is up to us. Let's take a two-dimensional array as an example (in fact, it is an official example ):

Defines a Matrix type Matrix. We can see from the use of var grid: Double [] That 7th rows define a one-dimensional Array to save all the data. Row 12th defines the access behavior of the underlying object, which can be get or set. Therefore, this subscript is of the "read/write" type. Based on the parameter row and column of the underlying object, the location of the element to be accessed in the Array can be calculated. The subscript of the Array can be used to directly read or write data to us.
Var myMatrix = Matrix (row: 2, columns: 2) myMatrix [0, 0] = 8
Println (myMatrix [0, 0]) // get 8
Now, we can use two-dimensional arrays happily ~~~ The same applies to multiple dimensions.

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.