Swift Tutorial - Tutorialspoint

(backadmin) #1
struct studentMarks {
var mark1 = 100
var mark2 = 200
var mark3 = 300
}
let marks = studentMarks()
println("Mark1 is \(marks.mark1)")
println("Mark2 is \(marks.mark2)")
println("Mark3 is \(marks.mark3)")

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


Mark1 is 100
Mark2 is 200
Mark3 is 300

Students marks are accessed by the structure name 'studentMarks'. The structure
members are initialized as mark1, mark2, mark3 with integer type values. Then the
structure studentMarks() is passed to the 'marks' with 'let' keyword. Hereafter 'marks' will
contain the structure member values. Now the values are printed by accessing the
structure member values by '.' with its initialized names.


struct MarksStruct {
var mark: Int

init(mark: Int) {
self.mark = mark
}
}
var aStruct = MarksStruct(mark: 98 )
var bStruct = aStruct // aStruct and bStruct are two structs with the same
value!
bStruct.mark = 97
println(aStruct.mark) // 98
println(bStruct.mark) // 97

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


98


97

Free download pdf