Swift Tutorial - Tutorialspoint

(backadmin) #1

}


}


let rectname = rectangle()

if let rectarea = rectname.print?.cprint {
println("Area of rectangle is \(rectarea)")
} else {
println("Rectangle Area is not specified")
}

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


Rectangle Area is not specified

Calling Methods Through Optional Chaining


class rectangle {
var print: circle?
}
class circle {
var area = [radius]()
var cprint: Int {
return area.count
}
subscript(i: Int) - > radius {
get {
return area[i]
}
set {
area[i] = newValue
}
}

func circleprint() {
println("Area of Circle is: \(cprint)")
}
Free download pdf