​Swift is just a powerful and spontaneous programming language for macOS, iOS, watchOS and tvO...

Page 206

Swift

}

struct mult { var a = sum() var b = diff() }

let calc = mult() println ("Inside mult block \(calc.a.num1, calc.a.num2)") println("Inside mult block \(calc.b.no1, calc.b.no2)")

let memcalc = mult(a: sum(num1: 300, num2: 500),b: diff(no1: 300, no2: 100))

println("Inside mult block \(memcalc.a.num1, memcalc.a.num2)") println("Inside mult block \(memcalc.b.no1, memcalc.b.no2)")

extension mult { init(x: sum, y: diff) { let X = x.num1 + x.num2 let Y = y.no1 + y.no2 } }

let a = sum(num1: 100, num2: 200) println("Inside Sum Block:\( a.num1, a.num2)")

let b = diff(no1: 200, no2: 100) println("Inside Diff Block: \(b.no1, b.no2)") When we run the above program using playground, we get the following result: Inside mult block (100, 200) Inside mult block (200, 100) Inside mult block (300, 500) Inside mult block (300, 100) 194


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.