swift-subscript Script (subscripts)

Source: Internet
Author: User
Tags instance method

  Subscript scripts can be defined in the classes (class), struct (struct), enumeration (enumeration) targets, can be considered as access to the collection, list or sequence shortcuts, use subscript script index settings and get values, do not need to invoke the instance of the specific assignment and access methods. Multiple subscript scripts can be defined for the same target, overloaded with different index value types, and subscript scripts are not limited to a single latitude, and you can define the requirements for the custom type of subscript scripts for multiple incoming parameters.

1. Syntax for subscript scripts

  The subscript script allows you to access and assign values to an instance by passing in one or more index values in the brackets behind the instance. Syntax is similar to the blending of instance methods and computed properties. Similar to defining an instance method, the subscript script is defined using the Subscript keyword, which displays the declaration in parameters (one or more) and the return type. Unlike instance methods, subscript scripts can be set to read-write or read-only. This approach is a bit like the getter and setter of computed properties:

Subscript (Index:int)->int {        get {            //Return the value of type INT that matches the entry parameter        }        set (NewValue) {            //execute assignment parameter        }    }

the type of newvalue must be the same as the return type defined by the subscript script. The same as the computed attribute is the parameter declaration of the set newvalue even if not written, the default newvalue variable can still be used to access the newly assigned value in the set code block. As with read-only computed properties, you can write the code that should be written in the get code block directly in subscript:

Subscript (Index:int)->int {        //returns the value of the Int type that matches the entry argument    }
2. Subscript Script Usage

  The subscript script also has different meanings depending on the usage scenario. Typically subscript scripts are shortcuts that are used to access the elements in a collection, list, or sequence. You can freely implement the subscript script in your own particular class or struct to provide the appropriate functionality.

For example, Swift's Dictionary (Dictionary) implements access to the values stored in its instance through the subscript script.

var numberoflegs = ["Spider": 8, "Ant": 6, "cat": 4]numberoflegs["bird"] = 4//stored value
3. Subscript Script Options

  The subscript script allows any number of parameter indexes, and there is no limit for each of the entry index types. The return value of the subscript script can also be any type.

swift-subscript Script (subscripts)

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.