Swift Tutorial - Tutorialspoint

(backadmin) #1
override init() {
super.init()
corners = 5
}

}

let bicycle = pentagon()
println("Pentagon: \(bicycle.description)")

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


Rectangle: 4 sides
Pentagon: 5 sides

Designated and Convenience Initializers in Action


class Planet {
var name: String

init(name: String) {
self.name = name
}

convenience init() {
self.init(name: "[No Planets]")
}
}
let plName = Planet(name: "Mercury")
println("Planet name is: \(plName.name)")

let noplName = Planet()
println("No Planets like that: \(noplName.name)")

class planets: Planet {
var count: Int
init(name: String, count: Int) {
self.count = count
Free download pdf