swift类型转换 – Swift最新教程

上一章Swift教程请查看:swift可选链式调用
在Swift 语言中“类型转换”用于验证实例类型,它用于检查实例类型是否属于某个特定的超类或子类,或者它是在自己的层次结构中定义的。
Swift类型转换提供了两个操作符:“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: "流体动力学", formulae: "Giga Hertz")] let samplechem = Chemistry(physics: "solid physics", equations: "Hertz") print("物理实例: \(samplechem.physics)") print("实例方程: \(samplechem.equations)") let samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz") print("物理实例: \(samplemaths.physics)") print("表达式实例: \(samplemaths.formulae)")

类型检查使用’ 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: "固体物理", equations: "Hertz"), Maths(physics: "流体动力学", formulae: "Giga Hertz"), Chemistry(physics: "热物理", equations: "Decibels"), Maths(physics: "宇宙物理学", formulae: "MegaHertz"), Maths(physics: "微分方程", formulae: "余弦级数")] let samplechem = Chemistry(physics: "固体物理", equations: "Hertz") print("物理实例: \(samplechem.physics)") print("实例方程: \(samplechem.equations)") let samplemaths = Maths(physics: "流体动力学", formulae: "Giga Hertz") print("物理实例: \(samplemaths.physics)") print("表达式实例: \(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, chemistry: \(chemCount), math: \(mathsCount)")

向下类型转换可以使用两个操作符(as?和as!)”as?当该值返回nil时,返回一个可选值,它用于检查成功的向下强制转换。
“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: "AA", equations: "Hertz"), Maths(physics: "BB", formulae: "Giga Hertz"), Chemistry(physics: "CC", equations: "Decibels"), Maths(physics: "DD", formulae: "MegaHertz"), Maths(physics: "EE", formulae: "Cosine Series")] let samplechem = Chemistry(physics: "AA", equations: "Hertz") print("物理实例: \(samplechem.physics)") print("方程实例: \(samplechem.equations)") let samplemaths = Maths(physics: "BB", formulae: "Giga Hertz") print("物理实例: \(samplemaths.physics)") print("表达式: \(samplemaths.formulae)") var chemCount = 0 var mathsCount = 0 for item in sa { if let print = item as? Chemistry { print("Chemistry: '\(print.physics)', \(print.equations)") } else if let example = item as? Maths { print("Maths: '\(example.physics)', \(example.formulae)") } }

类型转换:any和任何对象关键字“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: "固体物理", equations: "Hertz"), Maths(physics: "流体动力学", formulae: "Giga Hertz"), Chemistry(physics: "热力", equations: "Decibels"), Maths(physics: "宇宙学", formulae: "MegaHertz"), Maths(physics: "微分方程", formulae: "Cosine Series")] let samplechem = Chemistry(physics: "固体物理", equations: "Hertz") print("a: \(samplechem.physics)") print("b: \(samplechem.equations)") let samplemaths = Maths(physics: "流体动力学", formulae: "Giga Hertz") print("a: \(samplemaths.physics)") print("b: \(samplemaths.formulae)") var chemCount = 0 var mathsCount = 0 for item in sa { if let print = item as? Chemistry { print("Chemistry: '\(print.physics)', \(print.equations)") } else if let example = item as? Maths { print("Maths: '\(example.physics)', \(example.formulae)") } } var exampleany = [Any]() exampleany.append(12) exampleany.append(3.14159) exampleany.append("Any实例") exampleany.append(Chemistry(physics: "固体物理", equations: "Hertz")) for print in exampleany { switch print { case let someInt as Int: print("Integer: \(someInt)") case let someDouble as Double where someDouble > 0: print("Pi: \(someDouble)") case let someString as String: print("\(someString)") case let phy as Chemistry: print("Topics '\(phy.physics)', \(phy.equations)") default: print("None") } }

AnyObject【swift类型转换 – Swift最新教程】要表示任何类类型的实例,使用’ 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: "固体物理", equations: "Hertz"), Maths(physics: "流体动力学", 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: "固体物理", equations: "Hertz") print("Instance: \(samplechem.physics)") print("Instance: \(samplechem.equations)") let samplemaths = Maths(physics: "流体动力学", formulae: "Giga Hertz") print("Instance: \(samplemaths.physics)") print("Instance: \(samplemaths.formulae)") var chemCount = 0 var mathsCount = 0 for item in saprint { if let print = item as? Chemistry { print("Chemistry: '\(print.physics)', \(print.equations)") } else if let example = item as? Maths { print("Maths: '\(example.physics)', \(example.formulae)") } } var exampleany = [Any]() exampleany.append(12) exampleany.append(3.14159) exampleany.append("AnyObject") exampleany.append(Chemistry(physics: "固体物理", equations: "Hertz")) for print in exampleany { switch print { case let someInt as Int: print("v: \(someInt)") case let someDouble as Double where someDouble > 0: print("Pi: \(someDouble)") case let someString as String: print("\(someString)") case let phy as Chemistry: print("Topics '\(phy.physics)', \(phy.equations)") default: print("None") } }

    推荐阅读