Swift 在switch语句中测试类的类型
在Swift中,你可以在switch语句中使用is关键字来测试一个对象的类类型。此外,你还会有一些例子说明如何将一个对象的类型转换为预期的类型。
下面是一个测试原始数据类型的例子
在这个例子中,你将检查原始数据类型的类型,如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 block将执行。如果它是任何其他类型的实例,另一个案例块将被执行,如果它不是上述任何一种类型,默认块将被执行。
下面是一个测试自定义类型的例子
以类似的方式,你也可以检查自定义类型的类型。这意味着你可以检查由你创建的类的类型。
例子
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操作符的例子
在Swift中,还有一个关键词 “as”,你可以用来检查对象的类型。这个操作符是用来将一个对象从一种类型类型化为另一种类型。它返回一个所需类型的投射实例。
如果条件匹配为真,或者对象被转换为给定类型,则返回一个实例,否则返回nil,”如果 “条件将不执行。
例子
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语句。如果该对象不匹配任何一个类,它将执行默认块。你可以相应地处理代码的执行。否则,你可以用if-else语句检查。如果条件匹配,执行你想执行的动作。