为了验证的类型,"Type Casting"以Swift 4语言开始发挥作用,它用于检查类型是属于特定的超类还是子类,或者是否在其自己的层次结构中定义。
Swift 4类型转换提供两个运算符" is"来检查值的类型和" as",并将类型值转换为其他类型,类型转换还检查类型是否遵循特定的协议一致性标准。
类层结构
类型转换用于检查的类型,以查明它是否属于特定的类类型。另外,它检查类及其子类的层次结构以检查和转换这些,使其成为同一层次结构。
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 sa=[ Chemistry(physics: "solid physics", equations: "Hertz"), Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")] let samplechem=Chemistry(physics: "solid physics", equations: "Hertz") print("Instance physics is:\(samplechem.physics)") print("Instance equation is:\(samplechem.equations)") let samplemaths=Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz") print("Instance physics is:\(samplemaths.physics)") print("Instance formulae is:\(samplemaths.formulae)")
运行上述程序时,我们得到以下输出-
Instance physics is: solid physics Instance equation is: Hertz Instance physics is: Fluid Dynamics Instance formulae is: Giga Hertz
类型检查
类型检查是使用" is"运算符完成的。 " is"类型检查运算符检查是否属于特定的子类类型,如果该属于该,则返回" true",否则将返回" false"。
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 sa=[ 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") print("Instance physics is:\(samplechem.physics)") print("Instance equation is:\(samplechem.equations)") let samplemaths=Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz") print("Instance physics is:\(samplemaths.physics)") print("Instance formulae is:\(samplemaths.formulae)") var chemCount=0 var mathsCount=0 for item in sa { if item is Chemistry { ++chemCount } else if item is Maths { ++mathsCount } } print("Subjects in chemistry contains\(chemCount) topics and maths contains\(mathsCount) topics")
运行上述程序时,我们得到以下输出-
Instance physics is: solid physics Instance equation is: Hertz Instance physics is: Fluid Dynamics Instance formulae is: Giga Hertz Subjects in chemistry contains 2 topics and maths contains 3 topics
向下转换
向下转换子类类型可以使用两个运算符(as和as!)完成,当值返回nil时,返回一个可选值,它用于检查成功的向下转换。
'如!'当向下转换返回nil值时,返回可选解链中讨论的强制展开。在下行失败的情况下,它用于触发运行时错误
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 sa=[ 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") print("Instance physics is:\(samplechem.physics)") print("Instance equation is:\(samplechem.equations)") let samplemaths=Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz") print("Instance physics is:\(samplemaths.physics)") print("Instance formulae is:\(samplemaths.formulae)") var chemCount=0 var mathsCount=0 for item in sa { if let print=item as? Chemistry { print("Chemistry topics are: '\(print.physics)',\(print.equations)") } else if let example=item as? Maths { print("Maths topics are: '\(example.physics)',\(example.formulae)") } }
运行上述程序时,我们得到以下输出-
Instance physics is: solid physics Instance equation is: Hertz Instance physics is: Fluid Dynamics Instance formulae is: Giga Hertz Chemistry topics are: 'solid physics', Hertz Maths topics are: 'Fluid Dynamics', Giga Hertz Chemistry topics are: 'Thermo physics', Decibels Maths topics are: 'Astro Physics', MegaHertz Maths topics are: 'Differential Equations', Cosine Series
类型转换
关键字" Any"用于表示一个,该属于包括函数类型在内的任何类型。
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 sa=[ 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") print("Instance physics is:\(samplechem.physics)") print("Instance equation is:\(samplechem.equations)") let samplemaths=Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz") print("Instance physics is:\(samplemaths.physics)") print("Instance formulae is:\(samplemaths.formulae)") var chemCount=0 var mathsCount=0 for item in sa { if let print=item as? Chemistry { print("Chemistry topics are: '\(print.physics)',\(print.equations)") } else if let example=item as? Maths { print("Maths topics are: '\(example.physics)',\(example.formulae)") } } var exampleany=[Any]() exampleany.append(12) exampleany.append(3.14159) exampleany.append("Example for Any") exampleany.append(Chemistry(physics: "solid physics", equations: "Hertz")) for print in exampleany { switch print { case let someInt as Int: print("Integer value is\(someInt)") case let someDouble as Double where someDouble > 0: print("Pi value is\(someDouble)") case let someString as String: print("\(someString)") case let phy as Chemistry: print("Topics '\(phy.physics)',\(phy.equations)") default: print("None") } }
运行上述程序时,我们得到以下输出-
Instance physics is: solid physics Instance equation is: Hertz Instance physics is: Fluid Dynamics Instance formulae is: Giga Hertz Chemistry topics are: 'solid physics', Hertz Maths topics are: 'Fluid Dynamics', Giga Hertz Chemistry topics are: 'Thermo physics', Decibels Maths topics are: 'Astro Physics', MegaHertz Maths topics are: 'Differential Equations', Cosine Series Integer value is 12 Pi value is 3.14159 Example for Any Topics 'solid physics', Hertz
AnyObject
为了表示任何类类型的,使用了" AnyObject"关键字。
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") print("Instance physics is:\(samplechem.physics)") print("Instance equation is:\(samplechem.equations)") let samplemaths=Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz") print("Instance physics is:\(samplemaths.physics)") print("Instance formulae is:\(samplemaths.formulae)") var chemCount=0 var mathsCount=0 for item in saprint { if let print=item as? Chemistry { print("Chemistry topics are: '\(print.physics)',\(print.equations)") } else if let example=item as? Maths { print("Maths topics are: '\(example.physics)',\(example.formulae)") } } var exampleany=[Any]() exampleany.append(12) exampleany.append(3.14159) exampleany.append("Example for Any") exampleany.append(Chemistry(physics: "solid physics", equations: "Hertz")) for print in exampleany { switch print { case let someInt as Int: print("Integer value is\(someInt)") case let someDouble as Double where someDouble > 0: print("Pi value is\(someDouble)") case let someString as String: print("\(someString)") case let phy as Chemistry: print("Topics '\(phy.physics)',\(phy.equations)") default: print("None") } }
运行上述程序时,我们得到以下输出-
Instance physics is: solid physics Instance equation is: Hertz Instance physics is: Fluid Dynamics Instance formulae is: Giga Hertz Chemistry topics are: 'solid physics', Hertz Maths topics are: 'Fluid Dynamics', Giga Hertz Chemistry topics are: 'Thermo physics', Decibels Maths topics are: 'Astro Physics', MegaHertz Maths topics are: 'Differential Equations', Cosine Series Integer value is 12 Pi value is 3.14159 Example for Any Topics 'solid physics', Hertz
参考链接
https://www.learnfk.com/swift/swift-type-casting.html
标签:类型转换,String,Swift,无涯,Hertz,equations,formulae,print,physics From: https://blog.51cto.com/u_14033984/9472224