Swift2.0 Language Tutorial Subscript script

Source: Internet
Author: User

Swift2.0 Language Tutorial Subscript script subscript Script

Subscript scripts are a quick way to access objects, collections, or sequences. Developers do not need to invoke instance-specific assignments and access methods to directly access the required values. For example, in an array, you can directly use the subscript to access or modify an element in an array. The code is as follows:

    • Import Foundation
    • var array=["One", "one", "three", "four", "Five"]
    • Print ("Access element: \ (array[2])")
    • array[2]= "Third"
    • Print ("Access modified element: \ (array[2])")

The results of the operation are as follows:

    • Access element: Three
    • Accessing the modified element: third

In swift, subscript scripts can also be defined in a class. This allows the developer to quickly access the properties in the class, just like an array. This section will focus on how to use subscript scripts in your classes.

Defining the subscript Script

The subscript script is defined by the Subscript keyword, which is defined in the following form:

    • Subscript (Parameter name 1: Data type, parameter Name 2: Data type,...) returns the value of the data type {
    • get {
    • Returns the value of a type that matches a parameter type
    • }
    • Set (parameter name) {
    • Performing Assignment operations
    • }
    • }

Note: The set parameter name must be the same as the return value type defined by the subscript script, so do not specify a data type for it. As with the computed property, the default newvalue is used if the parameter is not declared after the set.

Example 8-18 the following defines a subscript script in the class that implements the value of a property by using the subscript script. The code is as follows:

  • Class newclass{
  • var english:int=0
  • var chinese:int=0
  • var math:int=0
  • Defining the subscript Script
  • subscript (index:int)->int{
  • get{
  • Switch index{
  • Case 0:
  • Return 中文版
  • Case 1:
  • Return Chinese
  • Case 2:
  • return Math
  • Default
  • return 0
  • }
  • }
  • set{
  • English=newvalue
  • Chinese=newvalue
  • Math=newvalue
  • }
  • }
  • }

Calling subscript Script

Once you have defined the subscript script, you can call it in the following form:

    • instance Object [Parameter 1, parameter 2,...]

where [] and what is inside it represents the subscript script defined in the class.

Use subscript Script

Subscript scripts can be divided into subscript scripts with one input parameter and subscript scripts with multiple parameter parameters, depending on the parameters passed in. The following is the use of these two subscript scripts in the class.

1. Subscript script with an incoming parameter

Subscript scripts with one entry parameter are the most common. The subscript used in the collection and in the string is the subscript script with an incoming parameter.

"Example 8-19" The following program calculates the and of 3 results by using the subscript script. The code is as follows:

  • Import Foundation
  • Class score{
  • var english:int=0
  • var chinese:int=0
  • var math:int=0
  • Defining the subscript Script
  • subscript (index:int)->int{
  • get{
  • Switch index{
  • Case 0:
  • Return 中文版
  • Case 1:
  • Return Chinese
  • Case 2:
  • return Math
  • Default
  • return 0
  • }
  • }
  • set{
  • English=newvalue
  • Chinese=newvalue
  • Math=newvalue
  • }
  • }
  • }
  • var myscore=score ()
  • var sum:int=0
  • var i:int=0
  • Traverse
  • For i=0;i<3;++i{
  • Sum+=myscore[i]
  • }
  • Print (sum)
  • modifying property values
  • myscore[0]=100
  • Myscore[1]=90
  • Myscore[2]=80
  • // Traverse Sum
  • For i=0;i<3;++i{
  • Sum+=myscore[i]
  • }
  • Print (sum)

The results of the operation are as follows:

    • 0
    • 240

Note: The subscript script can be set to read-write or read-only as computed properties. The above code is in the form of read and write. The general syntax of the read-only form is as follows:

    • Subscript (Parameter name: Data type), Int {
    • get{
    • Returns the value of type int that matches the parameter
    • }
    • }
    • Can be shortened to the following form:
    • Subscript (Parameter name: Data type), Int {
    • Returns the value of type int that matches the parameter
    • }

Example 8-20 follows the ability to use subscript to access property values using the Read-only form. The code is as follows:

  • Import Foundation
  • Class score{
  • var english:int=50
  • var chinese:int=100
  • var math:int=30
  • Defining the subscript Script
  • subscript (index:int)->int{
  • Switch index{
  • Case 0:
  • Return 中文版
  • Case 1:
  • Return Chinese
  • Case 2:
  • return Math
  • Default
  • return 0
  • }
  • }
  • }
  • var myscore=score ()
  • var sum:int=0
  • var i:int=0
  • Traversing output property values
  • For i=0;i<3;++i{
  • Print (Myscore[i])
  • }

The results of the operation are as follows:

    • 50
    • 100
    • 30

2. Subscript script with multiple parameters

Subscript scripts that have a single entry parameter are generally used in multidimensional dimension arrays. The following is an assignment of a two-dimensional array using subscripts with two parameters. The code is as follows:

  • Import Foundation
  • var value:int=0
  • Class newclass{
  • var rows:int = 0, columns:int=0
  • var grid: [Double]
  • Initialize method
  • Init (Rows:int, Columns:int) {
  • Self.rows = Rows
  • Self.columns = Columns
  • Grid = Array (count:rows * columns, repeatedvalue:0.0)
  • }
  • Func Indexisvalidforrow (Row:int, column:int), Bool {
  • Return row >= 0 && row < rows && column >= 0 && column < columns
  • }
  • Subscript Script
  • subscript (Row:int, column:int), Double {
  • get {
  • ASSERT (Indexisvalidforrow (row, Column:column), "Index Out of Range")
  • return grid[(Row * columns) + column]
  • }
  • set {
  • ASSERT (Indexisvalidforrow (row, Column:column), "Index Out of Range")
  • grid[(Row * columns) + column] = newvalue
  • }
  • }
  • }
  • var matrix = Newclass (Rows:2, Columns:2)
  • Print ("No Assignment Before")
  • Print (matrix[0,0])
  • Print (matrix[0,1])
  • Print (matrix[1,0])
  • Print (matrix[1,1])
  • Print ("After assignment")
  • matrix[0,0]=1.0
  • matrix[0,1]=5.6
  • matrix[1,0]=2.4
  • matrix[1,1]=3.2
  • Print (matrix[0,0])
  • Print (matrix[0,1])
  • Print (matrix[1,0])
  • Print (matrix[1,1])

The results of the operation are as follows:

    • Before assigning a value
    • 0.0
    • 0.0
    • 0.0
    • 0.0

After assigning a value

    • 1.0
    • 5.6
    • 2.4
    • 3.2

Of course, the subscript script can implement some custom functions in addition to accessing the object and the properties in the object, such as the following code, the function of this code is to calculate the product of subscript value and 10. The code is as follows:

  • Import Foundation
  • Class newclass{
  • var count1:int=10
  • Defining the subscript Script
  • subscript (index:int), Int {
  • get {
  • var count=index*count1
  • return count
  • }
  • Set (NewValue) {
  •               // Performing assignment Operations
  • Count1=newvalue
  • }
  • }
  • }
  • Let Newclass=newclass ()
  • Print (NEWCLASS.COUNT1)
  • Print (Newclass[6])

The results of the operation are as follows:

    • 10
    • 60

This article is selected from: Swift2.0 Language Quick Start v3.0 University bully Internal information, reproduced please indicate the source, respect the technology respect it people!

Swift2.0 Language Tutorial Subscript script

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.