Swift Tutorial - Tutorialspoint

(backadmin) #1
length = breadth * 10
}

init(frombre bre: Double) {
length = bre * 30
}

init(_ area: Double) {
length = area
}
}

let rectarea = Rectangle(180.0)
println("area is: \(rectarea.length)")

let rearea = Rectangle(370.0)
println("area is: \(rearea.length)")

let recarea = Rectangle(110.0)
println("area is: \(recarea.length)")

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


area is: Optional(180.0)
area is: Optional(370.0)
area is: Optional(110.0)

Modifying Constant Properties During Initialization


Initialization also allows the user to modify the value of constant property too. During
initialization, class property allows its class instances to be modified by the super class
and not by the subclass. Consider for example in the previous program 'length' is declared
as 'variable' in the main class. The below program variable 'length' is modified as 'constant'
variable.


struct Rectangle {
let length: Double?

init(frombreadth breadth: Double) {
Free download pdf