swift2.1で試してみたらできた。
let array: [Any?] = [1, "hoge", nil, UIViewController()]
for value in array {
switch value {
case let int as Int: print("Int: \(int)")
case let string as String: print("String: \(string)")
case nil: print("nil")
case let vc as UIViewController: print("UIViewController: \(vc)")
default: break;
}
}
case let [unwrap後の変数名] as [型名]
switch文の中にcase nil
も書けるためか、if let
の時と違ってas?
ではないみたい。
結果
Int: 1
String: hoge
nil
UIViewController: <UIViewController: 0x7fa190c2be10>