Swift Tutorial - Tutorialspoint

(backadmin) #1
"duration":"\(self.duration)"

]

}
}

var movie = film()
movie.head = "Swift Properties"
movie.duration = 3.09

println(movie.metaInfo["head"]!)
println(movie.metaInfo["duration"]!)

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


Swift Properties
3.09

Computed Properties as Property Observers


In Swift to observe and respond to property values Property Observers are used. Each and
every time when property values are set property observers are called. Except lazy stored
properties we can add property observers to 'inherited' property by method 'overriding'.


Property Observers can be defined by either


 Before Storing the value - willset

 After Storing the new value - didset

 When a property is set in an initializer willset and didset observers cannot be
called.

class Samplepgm {
var counter: Int = 0 {
willSet(newTotal){
println("Total Counter is: \(newTotal)")
}
didSet{
if counter > oldValue {
println("Newly Added Counter \(counter - oldValue)")
}
}
Free download pdf