is and as operators. is is used to detect the type of a value, and as is used to convert the type.\\n\\nType casting can also be used to check whether a class implements a certain protocol.\\n\\n* * *\\n\\n## Defining a Class Hierarchy\\n\\nThe following defines three classes: Subjects, Chemistry, and Maths. Chemistry and Maths inherit from Subjects.\\n\\nThe code is as follows:\\n\\nclass Subjects {\\n var physics: String\\n init(physics: String) {\\n self.physics = physics\\n }\\n}\\nclass Chemistry: Subjects {\\n var equations: String\\n init(physics: String, equations: String) {\\n self.equations = equations\\n super.init(physics: physics)\\n }\\n}\\nclass Maths: Subjects {\\n var formulae: String\\n init(physics: String, formulae: String) {\\n self.formulae = formulae\\n super.init(physics: physics)\\n }\\n}\\nlet sa = [\\n Chemistry(physics: "Solid-state Physics", equations: "Hertz"),\\n Maths(physics: "Fluid Dynamics", formulae: "Gigahertz")\\n]\\nlet samplechem = Chemistry(physics: "Solid-state Physics", equations: "Hertz")\\nprint("Example Physics is: \\\\(samplechem.physics)")\\nprint("Example Equation: \\\\(samplechem.equations)")\\nlet samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Gigahertz")\\nprint("Example Physics is: \\\\(samplemaths.physics)")\\nprint("Example Formula is: \\\\(samplemaths.formulae)")\\n\\nThe output of the above program execution is:\\n\\nExample Physics is: Solid-state Physics\\nExample Equation: Hertz\\nExample Physics is: Fluid Dynamics\\nExample Formula is: Gigahertz\\n\\n* * *\\n\\n## Checking Type\\n\\nType casting is used to detect whether an instance type belongs to a specific instance type.\\n\\nYou can use it on class and subclass hierarchies to check the type of a specific class instance and convert that class instance's type to another type within that hierarchy.\\n\\nType checking uses the is keyword.\\n\\nThe is operator checks whether an instance belongs to a specific subtype. If the instance belongs to that subtype, the type check operator returns true; otherwise, it returns false.\\n\\nclass Subjects {\\n var physics: String\\n init(physics: String) {\\n self.physics = physics\\n }\\n}\\nclass Chemistry: Subjects {\\n var equations: String\\n init(physics: String, equations: String) {\\n self.equations = equations\\n super.init(physics: physics)\\n }\\n}\\nclass Maths: Subjects {\\n var formulae: String\\n init(physics: String, formulae: String) {\\n self.formulae = formulae\\n super.init(physics: physics)\\n }\\n}\\nlet sa = [\\n Chemistry(physics: "Solid-state Physics", equations: "Hertz"),\\n Maths(physics: "Fluid Dynamics", formulae: "Gigahertz"),\\n Chemistry(physics: "Thermal Physics", equations: "Decibel"),\\n Maths(physics: "Astrophysics", formulae: "Megahertz"),\\n Maths(physics: "Differential Equation", formulae: "Cosine Series")\\n]\\nlet samplechem = Chemistry(physics: "Solid-state Physics", equations: "Hertz")\\nprint("Example Physics is: \\\\(samplechem.physics)")\\nprint("Example Equation: \\\\(samplechem.equations)")\\nlet samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Gigahertz")\\nprint("Example Physics is: \\\\(samplemaths.physics)")\\nprint("Example Formula is: \\\\(samplemaths.formulae)")\\nvar chemCount = 0\\nvar mathsCount = 0\\nfor item in sa {\\n // Returns true if it is an instance of the Chemistry type, otherwise it returns false.\\n if item is Chemistry {\\n ++chemCount\\n } else if item is Maths {\\n ++mathsCount\\n }\\n}\\nprint("The Chemistry subject contains \\\\(chemCount) subjectsοΌMath Includes \\\\(mathsCount) subjects")\\n\\nThe output of the above program execution is:\\n\\nExample Physics is: Solid-state Physics\\nExample Equation: Hertz\\nExample Physics is: Fluid Dynamics\\nExample Formula is: Gigahertz\\nChemistry subject contains 2 topics, Mathematics contains 3 topics\\n\\n* * *\\n\\n## Downcasting\\n\\nFor downcasting, use the type casting operators (as? or as!).\\n\\nWhen you are not sure if the downcast will succeed, use the conditional form of the type cast (as?). The conditional form of a type cast always returns an optional value, and if the downcast is not possible, the optional value will be nil.\\n\\nOnly use the forced form (as!) when you are certain that the downcast will succeed. When you try to downcast to an incorrect type, the forced form of the type cast will trigger a runtime error.\\n\\nclass Subjects {\\n var physics: String\\n init(physics: String) {\\n self.physics = physics\\n }\\n}\\nclass Chemistry: Subjects {\\n var equations: String\\n init(physics: String, equations: String) {\\n self.equations = equations\\n super.init(physics: physics)\\n }\\n}\\nclass Maths: Subjects {\\n var formulae: String\\n init(physics: String, formulae: String) {\\n self.formulae = formulae\\n super.init(physics: physics)\\n }\\n}\\nlet sa = [\\n Chemistry(physics: "Solid-state Physics", equations: "Hertz"),\\n Maths(physics: "Fluid Dynamics", formulae: "Gigahertz"),\\n Chemistry(physics: "Thermal Physics", equations: "Decibel"),\\n Maths(physics: "Astrophysics", formulae: "Megahertz"),\\n Maths(physics: "Differential Equation", formulae: "Cosine Series")\\n]\\nlet samplechem = Chemistry(physics: "Solid-state Physics", equations: "Hertz")\\nprint("Example Physics is: \\\\(samplechem.physics)")\\nprint("Example Equation: \\\\(samplechem.equations)")\\nlet samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Gigahertz")\\nprint("Example Physics is: \\\\(samplemaths.physics)")\\nprint("Example Formula is: \\\\(samplemaths.formulae)")\\nvar chemCount = 0\\nvar mathsCount = 0\\nfor item in sa {\\n // Conditional form of type casting\\n if let show = item as? Chemistry {\\n print("Chemistry topic is: '\\\\(show.physics)', \\\\(show.equations)")\\n // Forced form\\n } else if let example = item as? Maths {\\n print("Mathematics topic is: '\\\\(example.physics)', \\\\(example.formulae)")\\n }\\n}\\n\\nThe output of the above program execution is:\\n\\nExample Physics is: Solid-state Physics\\nExample Equation: Hertz\\nExample Physics is: Fluid Dynamics\\nExample Formula is: Gigahertz\\nChemistry topic is: 'Solid-state Physics', Hertz\\nMathematics topic is: 'Fluid Dynamics', Gigahertz\\nChemistry topic is: 'Thermal Physics', Decibel\\nMathematics topic is: 'Astrophysics', Megahertz\\nMathematics topic is: 'Differential Equation', Cosine Series\\n\\n* * *\\n\\n## Type Casting for Any and AnyObject\\n\\nSwift provides two special type aliases for uncertain types:\\n\\n* AnyObject can represent an instance of any class type.\\n* Any can represent any type, including function types.\\n\\n> Note:\\n> \\n> Only use Any and AnyObject\\n\\nwhen you explicitly need their behavior and functionality. It is always better to use the specific types you expect in your code.
\\n\\nAny Instances
\\n\\nclass Subjects {\\n var physics: String\\n init(physics: String) {\\n self.physics = physics\\n }\\n}\\nclass Chemistry: Subjects {\\n var equations: String\\n init(physics: String, equations: String) {\\n self.equations = equations\\n super.init(physics: physics)\\n }\\n}\\nclass Maths: Subjects {\\n var formulae: String\\n init(physics: String, formulae: String) {\\n self.formulae = formulae\\n super.init(physics: physics)\\n }\\n}\\nlet sa = [\\n Chemistry(physics: "Solid State Physics", equations: "Hertz"),\\n Maths(physics: "Fluid Dynamics", formulae: "Gigahertz"),\\n Chemistry(physics: "Thermal Physics", equations: "Decibel"),\\n Maths(physics: "Astrophysics", formulae: "Megahertz"),\\n Maths(physics: "Differential Equations", formulae: "Cosine Series")\\n]\\nlet samplechem = Chemistry(physics: "Solid State Physics", equations: "Hertz")\\nprint("Instance physics is: \\\\(samplechem.physics)")\\nprint("Instance equations: \\\\(samplechem.equations)")\\nlet samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Gigahertz")\\nprint("Instance physics is: \\\\(samplemaths.physics)")\\nprint("Instance formulae is: \\\\(samplemaths.formulae)")\\nvar chemCount = 0\\nvar mathsCount = 0\\n\\n
\\n
YouTip