当前位置:K88软件开发文章中心编程语言APP编程Swift01 → 文章内容

Swift 类型转换

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-12 6:31:29

Swift 类型转换Swift 语言类型转换可以判断实例的类型。也可以用于检测实例类型是否属于其父类或者子类的实例。Swift 中类型转换使用 is 和 as 操作符实现,is 用于检测值的类型,as 用于转换类型。类型转换也可以用来检查一个类是否实现了某个协议。定义一个类层次以下定义了三个类:Subjects、Chemistry、Maths,Chemistry 和 Maths 继承了 Subjects。代码如下: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: "赫兹"), Maths(physics: "流体动力学", formulae: "千兆赫")]let samplechem = Chemistry(physics: "固体物理", equations: "赫兹")print("实例物理学是: \(samplechem.physics)")print("实例方程式: \(samplechem.equations)")let samplemaths = Maths(physics: "流体动力学", formulae: "千兆赫")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: "赫兹"), Maths(physics: "流体动力学", formulae: "千兆赫"), Chemistry(physics: "热物理学", equations: "分贝"), Maths(physics: "天体物理学", formulae: "兆赫"), Maths(physics: "微分方程", formulae: "余弦级数")]let samplechem = Chemistry(physics: "固体物理", equations: "赫兹")print("实例物理学是: \(samplechem.physics)")print("实例方程式: \(samplechem.equations)")let samplemaths = Maths(physics: "流体动力学", formulae: "千兆赫")print("实例物理学是: \(samplemaths.physics)")print("实例公式是: \(samplemaths.formulae)")var chemCount = 0var mathsCount = 0for item in sa { // 如果是一个 Chemistry 类型的实例,返回 true,相反返回 false。 if item is Chemistry { ++chemCount } else if item is Maths { ++mathsCount }}print("化学科目包含 \(chemCount) 个主题,数学包含 \(mathsCount) 个主题")以上程序执行输出结果为:实例物理学是: 固体物理实例方程式: 赫兹实例物理学是: 流体动力学实例公式是: 千兆赫化学科目包含 2 个主题,数学包含 3 个主题向下转型向下转型,用类型转换操作符(as? 或 as!)当你不确定向下转型可以成功时,用类型转换的条件形式(as?)。条件形式的类型转换总是返回一个可选值(optional value),并且若下转是不可能的,可选值将是 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: "固体物理", equations: "赫兹"), Maths(physics: "流体动力学", formulae: "千兆赫"), Chemistry(physics: "热物理学", equations: "分贝"), Maths(physics: "天体物理学", formulae: "兆赫"), Maths(physics: "微分方程", formulae: "余弦级数")]let samplechem = Chemistry(physics: "固体物理", equations: "赫兹")print("实例物理学是: \(samplechem.physics)")print("实例方程式: \(samplechem.equations)")let samplemaths = Maths(physics: "流体动力学", formulae: "千兆赫")print("实例物理学是: \(samplemaths.physics)")print("实例公式是: \(samplemaths.formulae)")var chemCount = 0var mathsCount = 0for item in sa { // 类型转换的条件形式 if let show = item as? Chemistry { print("化学主题是: '\(show.physics)', \(show.equations)") // 强制形式 } else if let example = item as? Maths { print("数学主题是: '\(example.physics)', \(example.formulae)") }}以上程序执行输出结果为:实例物理学是: 固体物理实例方程式: 赫兹实例物理学是: 流体动力学实例公式是: 千兆赫化学主题是: '固体物理', 赫兹数学主题是: '流体动力学', 千兆赫化学主题是: '热物理学', 分贝数学主题是: '天体物理学', 兆赫数学主题是: '微分方程', 余弦级数

[1] [2]  下一页


Swift 类型转换