服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Swift - Swift编程中的一些类型转换方法详解

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

2020-12-23 15:40Swift教程网 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: "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 运行上面的程序,得到以下结果。

 

?
1
2
3
4
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")
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 运行上面的程序,得到以下结果。

 

?
1
2
3
4
5
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!)。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 运行上面的程序,得到以下结果。

 

?
1
2
3
4
5
6
7
8
9
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")
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 运行上面的程序,得到以下结果。

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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")
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 运行上面的程序,得到以下结果。

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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

 

延伸 · 阅读

精彩推荐