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`語句進行檢查。如果條件匹配,則執行您想要執行的操作。

更新於:2023年2月28日

2K+ 閱讀量

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.