Swift Tutorial - Tutorialspoint

(backadmin) #1
val.scaleBy( 3 )
val.scaleBy( 30 )
val.scaleBy( 300 )

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


9


15


270


450


81000


135000


Self Property for Mutating Method


Mutating methods combined with 'self' property assigns a new instance to the defined
method.


struct area {
var length = 1
var breadth = 1

func area() - > Int {
return length * breadth
}

mutating func scaleBy(res: Int) {
self.length *= res
self.breadth *= res
println(length)
println(breadth)
}
}
var val = area(length: 3 , breadth: 5 )
val.scaleBy( 13 )

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

Free download pdf