首页 > 编程 > Swift > 正文

Swift编程中的一些类型转换方法详解

2020-03-09 17:52:52
字体:
来源:转载
供稿:网友
这篇文章主要介绍了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: "Fluid Dynamics", formulae: "Giga Hertz")]


let samplechem = Chemistry(physics: "solid physics", equations: "Hertz")
println("Instance physics is: /(samplechem.physics)")
println("Instance equation is: /(samplechem.equations)")


let samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")
println("Instance physics is: /(samplemaths.physics)")
println("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")
println("Instance physics is: /(samplechem.physics)")
println("Instance equation is: /(samplechem.equations)")


let samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")
println("Instance physics is: /(samplemaths.physics)")
println("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
   }
}

println("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!)。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")
println("Instance physics is: /(samplechem.physics)")
println("Instance equation is: /(samplechem.equations)")


let samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")
println("Instance physics is: /(samplemaths.physics)")
println("Instance formulae is: /(samplemaths.formulae)")

var chemCount = 0
var mathsCount = 0

for item in sa {
   if let print = item as? Chemistry {
      println("Chemistry topics are: '/(print.physics)', /(print.equations)")
   } else if let example = item as? Maths {
      println("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")
println("Instance physics is: /(samplechem.physics)")
println("Instance equation is: /(samplechem.equations)")


let samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")
println("Instance physics is: /(samplemaths.physics)")
println("Instance formulae is: /(samplemaths.formulae)")

var chemCount = 0
var mathsCount = 0

for item in sa {
   if let print = item as? Chemistry {
      println("Chemistry topics are: '/(print.physics)', /(print.equations)")
   } else if let example = item as? Maths {
      println("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:
      println("Integer value is /(someInt)")
   case let someDouble as Double where someDouble > 0:
      println("Pi value is /(someDouble)")
   case let someString as String:
      println("/(someString)")
   case let phy as Chemistry:
      println("Topics '/(phy.physics)', /(phy.equations)")
   default:
      println("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', HertzAnyObject

为了表示是任何类型的类实例,使用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")
println("Instance physics is: /(samplechem.physics)")
println("Instance equation is: /(samplechem.equations)")


let samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")
println("Instance physics is: /(samplemaths.physics)")
println("Instance formulae is: /(samplemaths.formulae)")

var chemCount = 0
var mathsCount = 0

for item in saprint {
   if let print = item as? Chemistry {
      println("Chemistry topics are: '/(print.physics)', /(print.equations)")
   } else if let example = item as? Maths {
      println("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:
      println("Integer value is /(someInt)")
   case let someDouble as Double where someDouble > 0:
      println("Pi value is /(someDouble)")
   case let someString as String:
      println("/(someString)")
   case let phy as Chemistry:
      println("Topics '/(phy.physics)', /(phy.equations)")
   default:
      println("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


注:相关教程知识阅读请移步到swift教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表