Page 208


Mutating Instance Methods Instance methods can also be mutated when declared as extensions. Structure and enumeration methods that modify self or its properties must mark the instance method as mutating, just like mutating methods from an original implementation. extension Double { mutating func square() { let pi = 3.1415 self = pi * self * self } }

var Trial1 = 3.3 Trial1.square() println("Area of circle is: \(Trial1)")

var Trial2 = 5.8 Trial2.square() println("Area of circle is: \(Trial2)")

var Trial3 = 120.3 Trial3.square() println("Area of circle is: \(Trial3)") When we run the above program using playground, we get the following result: Area of circle is: 34.210935 Area of circle is: 105.68006 Area of circle is: 45464.070735

Subscripts Adding new subscripts to already declared instances can also be possible with extensions. extension Int { subscript(var multtable: Int) -> Int { 196

​Quick is really a powerful and intuitive programming language for macOS, iOS, watchOS and tvO...  


Read more
Read more
Similar to
Popular now
Just for you