Swift Tutorial - Tutorialspoint

(backadmin) #1

otherwise it will return some user defined print message by checking the statement 'if
circname.print?.circleprint() != nil'.


Accessing Subscripts through Optional Chaining


Optional chaining is used to set and retrieve a subscript value to validate whether call to
that subscript returns a value. '?' is placed before the subscript braces to access the
optional value on the particular subscript.


Program 1


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("The number of rooms is \(cprint)")
}
var rectarea: circumference?
}

class radius {
let radiusname: String
Free download pdf