Swift 类型转换


Swift 语言类型转换可以判断实例的类型。也可以用于检测实例类型是否属于其父类或者子类的实例。

Swift 中类型转换使用 is 和 as 操作符实现,is 用于检测值的类型,as 用于转换类型。

类型转换也可以用来检查一个类是否实现了某个协议。

定义一个类层次

以下定义了三个类:Subjects、Chemistry、Maths,Chemistry 和 Maths 继承了 Subjects。

代码如下:

  1. class Subjects {
  2. var physics: String
  3. init(physics: String) {
  4. self.physics = physics
  5. }
  6. }
  7.  
  8. class Chemistry: Subjects {
  9. var equations: String
  10. init(physics: String, equations: String) {
  11. self.equations = equations
  12. super.init(physics: physics)
  13. }
  14. }
  15.  
  16. class Maths: Subjects {
  17. var formulae: String
  18. init(physics: String, formulae: String) {
  19. self.formulae = formulae
  20. super.init(physics: physics)
  21. }
  22. }
  23.  
  24. let sa = [
  25. Chemistry(physics: "固体物理", equations: "赫兹"),
  26. Maths(physics: "流体动力学", formulae: "千兆赫")]
  27.  
  28.  
  29. let samplechem = Chemistry(physics: "固体物理", equations: "赫兹")
  30. print("实例物理学是: \(samplechem.physics)")
  31. print("实例方程式: \(samplechem.equations)")
  32.  
  33.  
  34. let samplemaths = Maths(physics: "流体动力学", formulae: "千兆赫")
  35. print("实例物理学是: \(samplemaths.physics)")
  36. print("实例公式是: \(samplemaths.formulae)")

以上程序执行输出结果为:

  1. 实例物理学是: 固体物理
  2. 实例方程式: 赫兹
  3. 实例物理学是: 流体动力学
  4. 实例公式是: 千兆赫

检查类型

类型转换用于检测实例类型是否属于特定的实例类型。

你可以将它用在类和子类的层次结构上,检查特定类实例的类型并且转换这个类实例的类型成为这个层次结构中的其他类型。

类型检查使用 is 关键字。

操作符 is 来检查一个实例是否属于特定子类型。若实例属于那个子类型,类型检查操作符返回 true,否则返回 false。

  1. class Subjects {
  2. var physics: String
  3. init(physics: String) {
  4. self.physics = physics
  5. }
  6. }
  7.  
  8. class Chemistry: Subjects {
  9. var equations: String
  10. init(physics: String, equations: String) {
  11. self.equations = equations
  12. super.init(physics: physics)
  13. }
  14. }
  15.  
  16. class Maths: Subjects {
  17. var formulae: String
  18. init(physics: String, formulae: String) {
  19. self.formulae = formulae
  20. super.init(physics: physics)
  21. }
  22. }
  23.  
  24. let sa = [
  25. Chemistry(physics: "固体物理", equations: "赫兹"),
  26. Maths(physics: "流体动力学", formulae: "千兆赫"),
  27. Chemistry(physics: "热物理学", equations: "分贝"),
  28. Maths(physics: "天体物理学", formulae: "兆赫"),
  29. Maths(physics: "微分方程", formulae: "余弦级数")]
  30.  
  31.  
  32. let samplechem = Chemistry(physics: "固体物理", equations: "赫兹")
  33. print("实例物理学是: \(samplechem.physics)")
  34. print("实例方程式: \(samplechem.equations)")
  35.  
  36.  
  37. let samplemaths = Maths(physics: "流体动力学", formulae: "千兆赫")
  38. print("实例物理学是: \(samplemaths.physics)")
  39. print("实例公式是: \(samplemaths.formulae)")
  40.  
  41. var chemCount = 0
  42. var mathsCount = 0
  43. for item in sa {
  44. // 如果是一个 Chemistry 类型的实例,返回 true,相反返回 false。
  45. if item is Chemistry {
  46. ++chemCount
  47. } else if item is Maths {
  48. ++mathsCount
  49. }
  50. }
  51.  
  52. print("化学科目包含 \(chemCount) 个主题,数学包含 \(mathsCount) 个主题")

以上程序执行输出结果为:

  1. 实例物理学是: 固体物理
  2. 实例方程式: 赫兹
  3. 实例物理学是: 流体动力学
  4. 实例公式是: 千兆赫
  5. 化学科目包含 2 个主题,数学包含 3 个主题

向下转型

向下转型,用类型转换操作符(as? 或 as!)

当你不确定向下转型可以成功时,用类型转换的条件形式(as?)。条件形式的类型转换总是返回一个可选值(optional value),并且若下转是不可能的,可选值将是 nil。

只有你可以确定向下转型一定会成功时,才使用强制形式(as!)。当你试图向下转型为一个不正确的类型时,强制形式的类型转换会触发一个运行时错误。

  1. class Subjects {
  2. var physics: String
  3. init(physics: String) {
  4. self.physics = physics
  5. }
  6. }
  7.  
  8. class Chemistry: Subjects {
  9. var equations: String
  10. init(physics: String, equations: String) {
  11. self.equations = equations
  12. super.init(physics: physics)
  13. }
  14. }
  15.  
  16. class Maths: Subjects {
  17. var formulae: String
  18. init(physics: String, formulae: String) {
  19. self.formulae = formulae
  20. super.init(physics: physics)
  21. }
  22. }
  23.  
  24. let sa = [
  25. Chemistry(physics: "固体物理", equations: "赫兹"),
  26. Maths(physics: "流体动力学", formulae: "千兆赫"),
  27. Chemistry(physics: "热物理学", equations: "分贝"),
  28. Maths(physics: "天体物理学", formulae: "兆赫"),
  29. Maths(physics: "微分方程", formulae: "余弦级数")]
  30.  
  31.  
  32. let samplechem = Chemistry(physics: "固体物理", equations: "赫兹")
  33. print("实例物理学是: \(samplechem.physics)")
  34. print("实例方程式: \(samplechem.equations)")
  35.  
  36.  
  37. let samplemaths = Maths(physics: "流体动力学", formulae: "千兆赫")
  38. print("实例物理学是: \(samplemaths.physics)")
  39. print("实例公式是: \(samplemaths.formulae)")
  40.  
  41. var chemCount = 0
  42. var mathsCount = 0
  43.  
  44. for item in sa {
  45. // 类型转换的条件形式
  46. if let show = item as? Chemistry {
  47. print("化学主题是: '\(show.physics)', \(show.equations)")
  48. // 强制形式
  49. } else if let example = item as? Maths {
  50. print("数学主题是: '\(example.physics)', \(example.formulae)")
  51. }
  52. }

以上程序执行输出结果为:

  1. 实例物理学是: 固体物理
  2. 实例方程式: 赫兹
  3. 实例物理学是: 流体动力学
  4. 实例公式是: 千兆赫
  5. 化学主题是: '固体物理', 赫兹
  6. 数学主题是: '流体动力学', 千兆赫
  7. 化学主题是: '热物理学', 分贝
  8. 数学主题是: '天体物理学', 兆赫
  9. 数学主题是: '微分方程', 余弦级数

Any和AnyObject的类型转换

Swift为不确定类型提供了两种特殊类型别名:

  • AnyObject可以代表任何class类型的实例。
  • Any可以表示任何类型,包括方法类型(function types)。

注意:
只有当你明确的需要它的行为和功能时才使用AnyAnyObject。在你的代码里使用你期望的明确的类型总是更好的。

Any 实例

  1. class Subjects {
  2. var physics: String
  3. init(physics: String) {
  4. self.physics = physics
  5. }
  6. }
  7.  
  8. class Chemistry: Subjects {
  9. var equations: String
  10. init(physics: String, equations: String) {
  11. self.equations = equations
  12. super.init(physics: physics)
  13. }
  14. }
  15.  
  16. class Maths: Subjects {
  17. var formulae: String
  18. init(physics: String, formulae: String) {
  19. self.formulae = formulae
  20. super.init(physics: physics)
  21. }
  22. }
  23.  
  24. let sa = [
  25. Chemistry(physics: "固体物理", equations: "赫兹"),
  26. Maths(physics: "流体动力学", formulae: "千兆赫"),
  27. Chemistry(physics: "热物理学", equations: "分贝"),
  28. Maths(physics: "天体物理学", formulae: "兆赫"),
  29. Maths(physics: "微分方程", formulae: "余弦级数")]
  30.  
  31.  
  32. let samplechem = Chemistry(physics: "固体物理", equations: "赫兹")
  33. print("实例物理学是: \(samplechem.physics)")
  34. print("实例方程式: \(samplechem.equations)")
  35.  
  36.  
  37. let samplemaths = Maths(physics: "流体动力学", formulae: "千兆赫")
  38. print("实例物理学是: \(samplemaths.physics)")
  39. print("实例公式是: \(samplemaths.formulae)")
  40.  
  41. var chemCount = 0
  42. var mathsCount = 0
  43.  
  44. for item in sa {
  45. // 类型转换的条件形式
  46. if let show = item as? Chemistry {
  47. print("化学主题是: '\(show.physics)', \(show.equations)")
  48. // 强制形式
  49. } else if let example = item as? Maths {
  50. print("数学主题是: '\(example.physics)', \(example.formulae)")
  51. }
  52. }
  53.  
  54. // 可以存储Any类型的数组 exampleany
  55. var exampleany = [Any]()
  56.  
  57. exampleany.append(12)
  58. exampleany.append(3.14159)
  59. exampleany.append("Any 实例")
  60. exampleany.append(Chemistry(physics: "固体物理", equations: "兆赫"))
  61.  
  62. for item2 in exampleany {
  63. switch item2 {
  64. case let someInt as Int:
  65. print("整型值为 \(someInt)")
  66. case let someDouble as Double where someDouble > 0:
  67. print("Pi 值为 \(someDouble)")
  68. case let someString as String:
  69. print("\(someString)")
  70. case let phy as Chemistry:
  71. print("主题 '\(phy.physics)', \(phy.equations)")
  72. default:
  73. print("None")
  74. }
  75. }

以上程序执行输出结果为:

  1. 实例物理学是: 固体物理
  2. 实例方程式: 赫兹
  3. 实例物理学是: 流体动力学
  4. 实例公式是: 千兆赫
  5. 化学主题是: '固体物理', 赫兹
  6. 数学主题是: '流体动力学', 千兆赫
  7. 化学主题是: '热物理学', 分贝
  8. 数学主题是: '天体物理学', 兆赫
  9. 数学主题是: '微分方程', 余弦级数
  10. 整型值为 12
  11. Pi 值为 3.14159
  12. Any 实例
  13. 主题 '固体物理', 兆赫

AnyObject 实例

  1. class Subjects {
  2. var physics: String
  3. init(physics: String) {
  4. self.physics = physics
  5. }
  6. }
  7.  
  8. class Chemistry: Subjects {
  9. var equations: String
  10. init(physics: String, equations: String) {
  11. self.equations = equations
  12. super.init(physics: physics)
  13. }
  14. }
  15.  
  16. class Maths: Subjects {
  17. var formulae: String
  18. init(physics: String, formulae: String) {
  19. self.formulae = formulae
  20. super.init(physics: physics)
  21. }
  22. }
  23.  
  24. // [AnyObject] 类型的数组
  25. let saprint: [AnyObject] = [
  26. Chemistry(physics: "固体物理", equations: "赫兹"),
  27. Maths(physics: "流体动力学", formulae: "千兆赫"),
  28. Chemistry(physics: "热物理学", equations: "分贝"),
  29. Maths(physics: "天体物理学", formulae: "兆赫"),
  30. Maths(physics: "微分方程", formulae: "余弦级数")]
  31.  
  32.  
  33. let samplechem = Chemistry(physics: "固体物理", equations: "赫兹")
  34. print("实例物理学是: \(samplechem.physics)")
  35. print("实例方程式: \(samplechem.equations)")
  36.  
  37.  
  38. let samplemaths = Maths(physics: "流体动力学", formulae: "千兆赫")
  39. print("实例物理学是: \(samplemaths.physics)")
  40. print("实例公式是: \(samplemaths.formulae)")
  41.  
  42. var chemCount = 0
  43. var mathsCount = 0
  44.  
  45. for item in saprint {
  46. // 类型转换的条件形式
  47. if let show = item as? Chemistry {
  48. print("化学主题是: '\(show.physics)', \(show.equations)")
  49. // 强制形式
  50. } else if let example = item as? Maths {
  51. print("数学主题是: '\(example.physics)', \(example.formulae)")
  52. }
  53. }
  54.  
  55. var exampleany = [Any]()
  56. exampleany.append(12)
  57. exampleany.append(3.14159)
  58. exampleany.append("Any 实例")
  59. exampleany.append(Chemistry(physics: "固体物理", equations: "兆赫"))
  60.  
  61. for item2 in exampleany {
  62. switch item2 {
  63. case let someInt as Int:
  64. print("整型值为 \(someInt)")
  65. case let someDouble as Double where someDouble > 0:
  66. print("Pi 值为 \(someDouble)")
  67. case let someString as String:
  68. print("\(someString)")
  69. case let phy as Chemistry:
  70. print("主题 '\(phy.physics)', \(phy.equations)")
  71. default:
  72. print("None")
  73. }
  74. }

以上程序执行输出结果为:

  1. 实例物理学是: 固体物理
  2. 实例方程式: 赫兹
  3. 实例物理学是: 流体动力学
  4. 实例公式是: 千兆赫
  5. 化学主题是: '固体物理', 赫兹
  6. 数学主题是: '流体动力学', 千兆赫
  7. 化学主题是: '热物理学', 分贝
  8. 数学主题是: '天体物理学', 兆赫
  9. 数学主题是: '微分方程', 余弦级数
  10. 整型值为 12
  11. Pi 值为 3.14159
  12. Any 实例
  13. 主题 '固体物理', 兆赫

在一个switch语句的case中使用强制形式的类型转换操作符(as, 而不是 as?)来检查和转换到一个明确的类型。