Swift Tutorial - Tutorialspoint

(backadmin) #1
counter.incrementBy(no1: 100 , no2: 5 )
counter.incrementBy(no1: 15000 , no2: 3 )

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


2400


500


45000


Self property in Methods


Methods have an implicit property known as 'self' for all its defined type instances. 'Self'
property is used to refer the current instances for its defined methods.


class calculations {
let a: Int
let b: Int
let res: Int

init(a: Int, b: Int) {
self.a = a
self.b = b
res = a + b
println("Inside Self Block: \(res)")
}

func tot(c: Int) - > Int {
return res - c
}

func result() {
println("Result is: \(tot(20))")
println("Result is: \(tot(50))")
}
}

let pri = calculations(a: 600 , b: 300 )
let sum = calculations(a: 1200 , b: 300 )
Free download pdf