Swift Tutorial - Tutorialspoint

(backadmin) #1

Another Example


struct markStruct{
var mark1: Int
var mark2: Int
var mark3: Int

init(mark1: Int, mark2: Int, mark3: Int){
self.mark1 = mark1
self.mark2 = mark2
self.mark3 = mark3
}
}

var fail = markStruct(mark1: 34 , mark2: 42 , mark3: 13 )

println(fail.mark1)
println(fail.mark2)
println(fail.mark3)

When we run the above program using playground, we get the following result:


34


42


13


The structure 'markStruct' is defined first with its members mark1, mark2 and mark3.
Now the variables of member classes are initialized to hold integer values. Then the copy
of the structure members are created with 'self' Keyword. Once the copy of the structure
members are created structure block with its parameter marks are passed to 'marks'
variable which will now hold the students marks. Then the marks are printed as 98, 96,



  1. Next step for the same structure members another instance named 'fail' is used to
    point the same structure members with different marks. Then the results are now printed
    as 34, 42, 13. This clearly explains that structures will have a copy of the member variables
    then pass the members to their upcoming function blocks.

Free download pdf