Swift Tutorial - Tutorialspoint

(backadmin) #1
self.length = length
self.breadth = breadth
area = length * breadth
}

init(fromLeng leng: Double, fromBread bread: Double) {
self.length = leng
self.breadth = bread
area = leng * bread
}
}

let ar = Rectangle(fromLength: 6 , fromBreadth: 12 )
println("area is: \(ar.area)")

let are = Rectangle(fromLeng: 36 , fromBread: 12 )
println("area is: \(are.area)")

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


area is: 72.0
area is: 432.0

Local & External Parameters


Initialization parameters have both local and global parameter names similar to that of
function and method parameters. Local parameter declaration is used to access within the
initialize body and external parameter declaration is used to call the initializer. Swift
initializers differ from function and method initializer that they do not identify which
initializer are used to call which functions.


To overcome this, Swift introduces an automatic external name for each and every
parameter in init(). This automatic external name is as equivalent as local name written
before every initialization parameter.


struct Days {
let sunday, monday, tuesday: Int
init(sunday: Int, monday: Int, tuesday: Int) {
self.sunday = sunday
self.monday = monday
Free download pdf