「Swift」type(of:)でクラスの型を判定するサンプル

2021年7月20日

書式
type(of:オブジェクト名)
使用例

class Person {
  let target="yuu"
}
 
// Personクラスを継承
class Student: Person {
    let str = "study"
}
 
let cft = Student()

//クラスの型
print(type(of: cft))
 //子クラス
print(cft is Student)
//スーパークラス
print(cft is Person) 

実行結果
Student
true
true

Swift

Posted by arkgame