有没有办法把这个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
仍然是 type 。该检查对施放它们没有任何影响。这迫使我在使用前强制施放。i
b
Any
is
as!
switch
有没有办法在一个语句中打开类型并将其绑定到名称?