Swift Tutorial - Tutorialspoint

(backadmin) #1

Best Usage Practices of Structures


Swift language provides the functionality to define structures as custom data types for
building the function blocks. The instances of structure are passed by its value to the
defined blocks for further manipulations.


Need for having structures


 To encapsulate simple data values.

 To copy the encapsulated data and its associated properties by 'values' rather than
by 'references'.

 Structure to 'Copy' and 'Reference'.

Structures in swift pass their members with their values rather than by its references.


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 marks = markStruct(mark1: 98 , mark2: 96 , mark3: 100 )
println(marks.mark1)
println(marks.mark2)
println(marks.mark3)

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


98


96


100

Free download pdf