gpt4 book ai didi

string - 模式匹配 (Any, Any) as (String, String) 在 Switch Case 中失败

转载 作者:搜寻专家 更新时间:2023-10-31 08:32:47 25 4
gpt4 key购买 nike

在以下代码方面需要帮助。

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

演示:http://swiftstub.com/65065637

最佳答案

据我所知,你做错了。

以下是我对您的代码所做的更改,以使其正常工作:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com