在以下代码方面需要帮助。
let first: Any = "One"
let second: Any = "Two"
let values = (first, second)
switch values {
case let (x, y) as (String, String):
print("Success", x, y)
default:
print("Failure")
}
switch first {
case let x as String:
print("Success", x)
default:
print("Failure")
}
--- 输出
Failure
Success One
--- 预期输出
Success One Two
Success One
最佳答案
据我所知,你做错了。
以下是我对您的代码所做的更改,以使其正常工作:
let first: Any = "One"
let second: Any = "Two"
let values = (first, second)
switch values {
case let (x as String, y as String):
print("Success", x, y)
default:
print("Failure")
}
switch first {
case let x as String:
print("Success", x)
default:
print("Failure")
}
输出:
Success One Two
Success One
希望这对您有所帮助!
关于string - 模式匹配 (Any, Any) as (String, String) 在 Switch Case 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34647334/