Page 201

Swift

AnyObject To represent the instance of any class type, 'AnyObject' keyword is used. class Subjects { var physics: String init(physics: String) { self.physics = physics } }

class Chemistry: Subjects { var equations: String init(physics: String, equations: String) { self.equations = equations super.init(physics: physics) } }

class Maths: Subjects { var formulae: String init(physics: String, formulae: String) { self.formulae = formulae super.init(physics: physics) } }

let saprint: [AnyObject] = [Chemistry(physics: "solid physics", equations: "Hertz"), Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz"), Chemistry(physics: "Thermo physics", equations: "Decibels"), Maths(physics: "Astro Physics", formulae: "MegaHertz"), Maths(physics: "Differential Equations", formulae: "Cosine Series")]

let samplechem = Chemistry(physics: "solid physics", equations: "Hertz") println("Instance physics is: \(samplechem.physics)") 189

​Swift is just a effective and user-friendly programming language for macOS, iOS, watchOS and ...  

...

Advertisement