Tag站长知识库:分享、传递知识使我们更快乐,更幸福,更和谐!

最近更新热门图文热门文章全站推荐Tag标签网站地图
您现在的位置:首页 > 程序开发 > Swift开发>>Swift类型转换

Swift类型转换

2019-06-17 17:21作者:佚名来源:易百教程浏览:25 评论:25

在Swift 4语言中类型转换用于检查实例类型是属于特定的超类还是子类,还是在自己的层次结构中定义。

Swift 4类型转换提供两个运算符isas来检查值的类型,并将类型值转换为不同的类型。 类型转换还检查实例类型是否遵循特定的协议一致性标准。

定义类层次结构

类型转换用于检查实例的类型,以确定它是否属于特定的类类型。 此外,它还检查类及其子类的层次结构,以检查并转换这些实例,使其成为同一层次结构。

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)")

当使用playground 运行上述程序时,得到以下结果 -

Instance physics is: solid physicsInstance equation is: HertzInstance physics is: Fluid DynamicsInstance 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 = 0var mathsCount = 0for 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")

当使用playground 运行上述程序时,得到以下结果 -

Instance physics is: solid physicsInstance equation is: HertzInstance physics is: Fluid DynamicsInstance formulae is: Giga HertzSubjects in chemistry contains 2 topics and maths contains 3 topics

向下转换

向下转换子类类型可以使用两个运算符(as??)。as?当值返回nil时返回可选值。 它用于检查成功的向下转换。

as!当向下转换返回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 = 0var mathsCount = 0for 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)")   }}

当使用playground运行上述程序时,得到以下结果 -

Instance physics is: solid physicsInstance equation is: HertzInstance physics is: Fluid DynamicsInstance formulae is: Giga HertzChemistry topics are: 'solid physics', HertzMaths topics are: 'Fluid Dynamics', Giga HertzChemistry topics are: 'Thermo physics', DecibelsMaths topics are: 'Astro Physics', MegaHertzMaths 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 = 0var mathsCount = 0for 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")   }}

当使用playground运行上述程序时,得到以下结果 -

Instance physics is: solid physicsInstance equation is: HertzInstance physics is: Fluid DynamicsInstance formulae is: Giga HertzChemistry topics are: 'solid physics', HertzMaths topics are: 'Fluid Dynamics', Giga HertzChemistry topics are: 'Thermo physics', DecibelsMaths topics are: 'Astro Physics', MegaHertzMaths topics are: 'Differential Equations', Cosine SeriesInteger value is 12Pi value is 3.14159Example for AnyTopics '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 = 0var mathsCount = 0for 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")   }}

当使用playground运行上述程序时,得到以下结果 -

Instance physics is: solid physicsInstance equation is: HertzInstance physics is: Fluid DynamicsInstance formulae is: Giga HertzChemistry topics are: 'solid physics', HertzMaths topics are: 'Fluid Dynamics', Giga HertzChemistry topics are: 'Thermo physics', DecibelsMaths topics are: 'Astro Physics', MegaHertzMaths topics are: 'Differential Equations', Cosine SeriesInteger value is 12Pi value is 3.14159Example for AnyTopics 'solid physics', Hertz

Tags: 责任编辑:Tag站长知识库
顶一下(25)
87.08%

精彩信息

     

精彩信息

     

精彩信息