Swift Programming Language-Subscripts and swiftlanguage
The affiliated scripts can be defined in objects such as Class, structure, and enumeration. They can be considered as shortcuts to access objects, sets, or sequences, you do not need to call the instance's specific assignment and access methods. For example, to access elements in an Array (Array) instance using a ancillary script, you can write someArray [index] in this way, and to access elements in a Dictionary instance, you can write someDictionary [key] in this way.
For the same target, you can define multiple ancillary scripts and perform heavy loads based on different index value types, and the number of index values can be multiple.
The attached script overload is not demonstrated in this section.
Ancillary script syntax
The affiliated script allows you to access and assign values to an instance by inputting one or more index values in square brackets after the instance. The syntax is similar to the mixing of instance methods and computing attributes. Similar to the instance definition method, the custom scripts use the subscript keyword to explicitly declare input parameters (one or more) and return types. Different from the instance method, the affiliated scripts can be set to read/write or read-only. This method is a bit like getter and setter of the compute attribute:
Subscript (index: Int)-> Int {get {// return the Int type value that matches the input parameter} set (newValue) {// perform the value assignment operation }}
The newValue type must be the same as the return type defined by the affiliated script. The same as the computing attribute is that even if newValue is not written, the default newValue variable can still be used in the set code block to access the newly assigned value.
Like read-only computing attributes, you can directly write the code originally written in the get code block to subscript:
Subscript (index: Int)-> Int {// return the Int type value that matches the input parameter}
The following code demonstrates the usage of a read-only ancillary script in the TimesTable struct, which is used to display n times of the input integer.
Struct TimesTable {let multiplier: Int subscript (index: Int)-> Int {return multiplier * index} let threeTimesTable = TimesTable (multiplier: 3) println ("6 times of 3 is \ (threeTimesTable [6])") // output "6 times of 3 is 18"
In the preceding example, an instance is created using the TimesTable struct to indicate three times the index value. Value 3 is used as the struct constructor input parameter to initialize the instance Member multiplier.
You can obtain the result through the affiliated script, such as threeTimesTable [6]. This statement accesses the sixth element of threeTimesTable and returns 3 times of 18 or 6.
Note:
The TimesTable example is based on a fixed mathematical formula. It is not suitable for assigning values to threeTimesTable [someIndex] with open write permissions. This is why the affiliated scripts are only defined as read-only.
Affiliated script usage
Scripts attached to different use cases have different meanings. A companion script is a shortcut used to access elements in a collection, list, or sequence. You can freely implement the affiliated scripts in your own specific classes or struct to provide the appropriate functions.
For example, the Swift Dictionary allows users to access the values stored in their instances through the affiliated scripts. Use a value of the same type as the dictionary index in the affiliated script, and assign a value of the dictionary value type to this affiliated script to set a value for the dictionary:
var numberOfLegs = ["spider": 8,"ant": 6, "cat": 4]numberOfLegs["bird"] = 2
The preceding example defines a variable named numberOfLegs and initializes a dictionary instance containing three pairs of key values using a dictionary literal. The Dictionary storage value type of numberOfLegs is inferred to be Dictionary <String, Int>. After the dictionary instance is created, the integer value 2 is assigned to the index of the dictionary instance to the position of bird by means of the affiliated script.