Accesses the element members of a collection, which can use subscripts in sequences and lists, structs, and enumerations in a class. These subscripts are used to store and use indexes to retrieve values. Array elements can be used such as: Somearray[index], and access to Dictionary instances and their subsequent member elements can also be used as: Somedicitonary[key].
For a single type, the subscript range can be from single to multiple declarations. We can pass the appropriate subscript overload to the type of the subscript index value. The subscript can also declare a range from a single dimension to a multidimensional degree based on the input data type.
Subscript declaration Syntax and usage
Let's review the properties of the calculation. The subscript also follows the same syntax as the computed property. An instance of a query type is enclosed in square brackets, followed by an instance name. The following banner follows a structure as the same syntax as instance method and calculated property. The "subscript" keyword is used to define the label, and the user can specify one or more parameters with a return type. Subscript can have read-write or read-only properties and instance storage and retrieval using the getter and Setter properties as calculated properties.
Grammar
Copy Code code as follows:
Subscript (index:int)->int{get{//used for subscript value Declarations}set (newvalue) {//definitions are written }}
Example 1
Copy Code code as follows:
struct Subexample {let decrementer:int
Subscript (index:int)->int{return Decrementer/index
}}let Division = subexample (decrementer:100)
println ("The number is divisible by \ (Division[9])")
println ("The number is divisible by \ (division[2])")
println ("The number is divisible by \ (Division[3])")
println ("The number is divisible by \ (Division[5])")
println ("The number is divisible by \ (Division[7])")
When we use playground to run the above program, we get the following results
The number is divisible by-times the number was divisible by the number is divisible by-times
Th E number is divisible by the ' number is '
divisible by
Example 2
Copy Code code as follows:
Class Daysofaweek {Privatevar days =["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
Subscript (Index:int)->string{get{return Days[index]}set (newvalue) {self.days[index]= NewValue
}}}var p = Daysofaweek ()
println (P[0])
println (P[1])
println (P[2])
println (P[3])
When we use playground to run the above program, we get the following results
Sunday
Monday
Tuesday
Wednesday
Subscript Options
subscript uses a single to multiple input parameters, which are also arbitrary data types. You can also use variables, parameters for variable parameters. The subscript cannot provide a default parameter value or use any of the in-out parameters.
Defining multiple labels called "subscript overloads" can provide multiple subscript definitions as needed on a class or struct. These multiple marks are based on the type inference that the value is declared within the subscript bracket.
Copy Code code as follows:
Structmatrix{let Rows:int, columns:intvarprint:[double]
Init (Rows:int, columns:int) {self.rows = rows
Self.columns = Columns
Print=array (count:rows * columns, repeatedvalue:0.0)}
Subscript (Row:int, Column:int)->double{get{returnprint[(Row * columns) + column]}set{print[(Row * columns) + column] = NewValue
}}}var Mat =matrix (Rows:3, Columns:3)
mat[0,0]=1.0
mat[0,1]=2.0
mat[1,0]=3.0
mat[1,1]=5.0
println ("\ (mat[0,0])")
println ("\ (mat[0,1])")
println ("\ (mat[1,0])")
println ("\ (mat[1,1])")
When we use playground to run the above program, we get the following results
Swift subscript supports single parameter to multiple parameter declaration of the corresponding data type. The "matrix" structure declared by this program is the 2*2 dimension array matrix to store the "Double" data type. The matrix parameter is entered as an integer data type to declare rows and columns.
New instances of matrices are created by initializing rows and columns, as shown below.
Copy Code code as follows:
var mat = Matrix (Rows:3, Columns:3)
A matrix value can be defined as follows by passing the row and column values to the subscript, separated by commas.
Copy Code code as follows:
mat[0,0] = 1.0
mat[0,1] = 2.0
mat[1,0] = 3.0
mat[1,1] = 5.0
Definition of subscript method
the definition syntax for the subscript method is similar to the syntax of the instance method and the computed property.
The subscript method uses the Subscript keyword to indicate that it is a subscript method. As with instance methods, the Subscript method definition can specify one or more input parameters, with a return type. Unlike an instance method, the subscript method can be read or write-readable. As with the definition of computed properties, the subscript method conveys the behavior of the subscript method by using getter and setter. If the getter and setter are specified in the definition of the subscript method, defines a read-write subscript method that, if the definition of the subscript method does not contain a setter, defines a reading-only subscript method, and the keyword get that represents the getter method can also be omitted. The complete syntax for the subscript method definition is as follows:
Copy Code code as follows:
Subscript (Index:int)->int {
get {
Return a appropriate subscript value here
}
Set (NewValue) {
Perform a suitable setting action here
}
}
This example defines a read-write subscript method that can be specified, newvalue, and not specified, using the default parameter name NewValue.
The following example shows the definition and use of a read-only subscript method. Because the read-only subscript method specifies only one getter, the meaning is unambiguous, so you can omit the Get keyword.
struct Timestable {
Let Multiplier:int
Subscript (Index:int)->int {
Return multiplier *index
}
}
Let Threetimestable =timestable (Multiplier:3)
println ("six times three Is\ (Threetimestable[6])")
Prints "six times three is 18"
use of the subscript method
You can define and implement multiple subscript methods for a type, and the compiler infers and selects the appropriate subscript method based on the type of index parameter that you send to the subscript method.
Like a method, the subscript method can contain any number of input parameters, and the type of the input parameter can be of any type, and the subscript method can return any type. The subscript method can also use variable parameters, but cannot use in-out parameters or default parameter values.
If you can define a multiple-latitude subscript method with multiple input parameters, the following example shows how to define a subscript method with two integer types for a matrix structure and how to use the defined subscript method to index the two-latitude elements defined in the matrix.
Copy Code code as follows:
struct Matrix {
Let Rows:int,columns:int
var grid:double[]
Init (rows:int,columns:int) {
Self.rows =rows
Self.columns =columns
Grid =array (Count:rows *columns,repeatedvalue:0.0)
}
Subscript (Row:int,column:int)->double {
get {
Return grid[(Row *columns) +column]
}
set {
grid[(Row *columns) +column] =newvalue
}
}
}
var matrix =matrix (Rows:2,columns:2)
matrix[0,1] =1.5
matrix[1,0] =3.2