Swift:在switch语句中测试类类型
在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”,您可以使用它来检查对象类型。此运算符用于将对象从一种类型强制转换为另一种类型。它返回所需类型的强制转换实例。
如果条件匹配为真或对象是给定类型的强制转换,则返回一个实例;否则返回`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”关键字来检查类对象的类型。很多时候,您可能需要对象类型来执行操作或相应地执行代码。
如果您想检查多个类的对象,您可以使用带有不同`case`的`switch`语句。如果对象与任何类都不匹配,它将执行`default`块。您可以相应地处理代码执行。否则,您可以使用`if-else`语句进行检查。如果条件匹配,则执行您想要执行的操作。
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP