Swift Tutorial - Tutorialspoint

(backadmin) #1
println("\(mat[0,0])")
println("\(mat[0,1])")
println("\(mat[1,0])")
println("\(mat[1,1])")

When we run the above program using playground, we get the following 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
Free download pdf