ifこの//はしごを switch 文として書く方法はありますelse ifか?else
let x: Any = "123"
if let s = x as? String {
useString(s)
}
else if let i = x as? Int {
useInt(i)
}
else if let b = x as? Bool {
useBool(b)
}
else {
fatalError()
}
これが私の試みです:
switch x {
case let s where s is String: useString(s)
case let i where i is Int: useInt(i)
case let b where b is Bool: useBool(b)
default: fatalError()
}
正しいパスが正常に選択されますが、 //はsまだタイプです。チェックはそれらをキャストする際には何の効果もありません。これにより、使用前にキャストを強制する必要があります。ibAnyisas!
タイプをオンにして名前にバインドする方法はありswitchますか?