Swift:在 switch 语句中测试类类型

swiftserver side programmingprogramming更新于 2025/9/13 16:37:17

在 Swift 中,您可以使用 is 关键字在 switch 语句中测试对象的类类型。此外,您还可以看到一些如何将对象强制转换为预期类型的示例。

这是一个测试原始数据类型的示例

在此示例中,您将检查原始数据类型的类型,例如 String、Int、Double 等。您可以使用 switch 语句检查多个条件,如下所示 -

示例

import Foundation
func checkType(_ value: Any) {
   switch value {
      case is String: print("The value is String.")
      case is Int: print("The value is Int.")
      case is Int32: print("The value is Int32.")
      case is Double: print("The value is Double.")
      case is Bool: print("The value is Boolean.")
      default: print("The value is of some other type.")
   }
}
checkType(16)
checkType(16.0)
checkType("16")
checkType(true)
checkType(Float(16.0))

输出

The value is Int.
The value is Double.
The value is String.
The value is Boolean.
The value is of some other type.

在此示例中,值是"Any"类型的实例,它将使用 switch 语句的关键字来测试值的原始类型。如果值是 String 的实例,则执行第一个 case 块。如果它是任何其他类型的实例,则执行另一个 case 块;如果它不是上述任何一种,则执行 default 块。

这是一个测试自定义类型的示例

类似地,您也可以检查自定义类型的类型。这意味着您可以检查您创建的类类型。

示例

import Foundation
class Car {  
   let numberOfWheels: Int
   let colorCode: String
    
   init(wheels: Int, color: String) {
      self.numberOfWheels = wheels
      self.colorCode = color
   }
}
class Person {
   let name: String
   let age: Int
    
   init(name: String, age: Int) {
      self.name = name
      self.age = age
   }
}
class Student {
   var school: String
   var grade: Int
    
   init(school: String, grade: Int) {
      self.school = school
      self.grade = grade
   }
}
func checkType(_ object: Any) {
   switch object {
      case is Car: print("The object is a Car.")
      case is Person: print("The object is a Person.")
      case is Student: print("The object is a Student.")
      default: print("The object is of some other type.")
   }
}
let car = Car(wheels: 4, color: "red")
let person = Person(name: "Amit Yadav", age: 23)
let student = Student(school: "St. Primary School", grade: 5)
checkType(car)
checkType(student)
checkType(person)

输出

The object is a Car.
The object is a Student.
The object is a Person.

如果您看到这两个示例,我们指的是检查给定对象或值的类型。但是,如果您想将给定对象强制转换为预期类型并在稍后使用,建议使用 as 运算符进行类型转换。使用此方法,您可以强制转换对象以访问其属性。

以下是使用 as 运算符的示例

在 Swift 中,还有另一个关键字"as"可用于检查对象类型。此运算符用于将对象从一种类型强制转换为另一种类型。它返回所需类型的强制转换实例。

如果条件为 true 或对象强制转换为给定类型,则返回一个实例,否则返回 nil,并且"if"条件将不会执行。

示例

import Foundation
class Car {    
   let numberOfWheels: Int
   let colorCode: String
    
   init(wheels: Int, color: String) {
      self.numberOfWheels = wheels
      self.colorCode = color
   }
}
class Person {
   let name: String
   let age: Int
    
   init(name: String, age: Int) {
      self.name = name
      self.age = age
   }
}
class Student {
   var school: String
   var grade: Int
    
   init(school: String, grade: Int) {
      self.school = school
      self.grade = grade
   }
}
func checkType(_ object: Any) {
   switch object {       
      case let carObject as Car:
         print("The object is a Car type with \(carObject.numberOfWheels) wheels.")   
      case let personObject as Person:
         print("The object is a Person type with the name \(personObject.name).")       
      case let studentObject as Student:
         print("The object is a Student type with school \(studentObject.school).")
      default: print("The object is of some other type.")
   }
}
let car = Car(wheels: 4, color: "red")
let person = Person(name: "Amit Yadav", age: 23)
let student = Student(school: "St. Primary School", grade: 5)
checkType(car)
checkType(student)
checkType(person)

输出

The object is a Car type with 4 wheels.
The object is a Student type with school St. Primary School.
The object is a Person type with the name Amit Yadav.

结论

在 Swift 中,您可以使用"is"关键字来检查类对象的类型。很多时候,您可能需要指定对象类型来执行某个操作或相应的代码。

如果您想检查一个对象是否符合多个类,可以使用 switch 语句并结合不同的 case 条件。如果对象不匹配任何类,它将执行 default 代码块。您可以相应地处理代码的执行。否则,您可以使用 if-else 语句进行检查。如果条件匹配,则执行您想要的操作。



相关文章



关于我们 | 隐私条款 | 学习路线
返回顶部