Accessing the element members of a
collection, sequence and a list in Classes, Structures and Enumerations are
carried out with the help of subscripts. These subscripts are used to store and
retrieve the values with the help of index. Array elements are accessed with
the help of someArray[index] and its subsequent member elements in a Dictionary
instance can be accessed as someDicitonary[key].
For a single type, subscripts can
range from single to multiple declarations. We can use the appropriate
subscript to overload the type of index value passed to the subscript.
Subscripts also ranges from single dimension to multiple dimension according to
the users requirements for their input data type declarations.
Subscript Declaration Syntax and its Usage
Let's have a recap to the computed
properties. Subscripts too follow the same syntax as that of computed
properties. For querying type instances, subscripts are written inside a square
bracket followed with the instance name. Subscript syntax follows the same
syntax structure as that of 'instance method' and 'computed property' syntax.
'subscript' keyword is used for defining subscripts and the user can specify
single or multiple parameters with their return types. Subscripts can have
read-write or read-only properties and the instances are stored and retrieved
with the help of 'getter' and 'setter' properties as that of
computed properties.
Syntax
subscript(index: Int) -> Int {
get {
// used for
subscript value declarations
}
set(newValue) {
//
definitions are written here
}
}
Example1
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]) times")
println("The number is divisible by \(division[2]) times")
println("The number is divisible by \(division[3]) times")
println("The number is divisible by \(division[5]) times")
println("The number is divisible by \(division[7]) times")
Result:
The
number is divisible by 11 times
The
number is divisible by 50 times
The
number is divisible by 33 times
The
number is divisible by 20 times
The
number is divisible by 14 times
Example2
class daysofaweek {
private var 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])
Result:
Sunday
Monday
Tuesday
Wednesday
Options in Subscript
Subscripts takes single to multiple
input parameters and these input parameters also belong to any datatype. They
can also use variable and variadic parameters. Subscripts cannot provide
default parameter values or use any in-out parameters.
Defining multiple subscripts are
termed as 'subscript overloading' where a class or structure can provide
multiple subscript definitions as required. These multiple subscripts are
inferred based on the types of values that are declared within the subscript
braces.
struct Matrix {
let rows: Int, columns: Int
var print: [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 {
return print[(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])")
Result:
1.0
2.0
3.0
5.0
Swift subscript supports single
parameter to multiple parameter declarations for appropriate data types. The
program declares 'Matrix' structure as a 2 * 2 dimensional array matrix to
store 'Double' data types. The Matrix parameter is inputted with Integer data
types for declaring rows and columns.
New instance for the Matrix is
created by passing row and column count to the initialize as shown below.
var
mat = Matrix(rows: 3, columns: 3)
Matrix values can be defined by
passing row and column values into the subscript, separated by a comma as shown
below.
mat[0,0]
= 1.0
mat[0,1]
= 2.0
mat[1,0]
= 3.0
mat[1,1]
= 5.0
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.