2017-11-01 20 views

答えて

2

あなたはtype(of:)を使用して要素の型を印刷することができます。

let arr:[Any] = [1, "wo", true] 
arr.forEach { (element) in 
    print(String(describing: type(of: element))) 
} 

出力
のInt
文字列
ブール

それとも「であるとスイッチケースを使用することができます"

arr.forEach { (element) in 
    switch element { 
    case is Int: 
     print("its a Int") 
    case is String: 
     print("its a String") 
    case is Bool: 
     print("its a Bool") 
    default: 
     print("unspecified") 
    } 
} 

出力
にそのがint
その文字列
そのブール

関連する問題