在Switch Case中,模式匹配(任意,任意)为(字符串,字符串)失败

时间:2016-01-07 04:21:17

标签: string swift switch-statement swift2

在以下代码中需要帮助。

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

1 个答案:

答案 0 :(得分:4)

据我所知,你做错了。

以下是我对您的代码所做的更改,以便它可以正常运行:

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

希望这有帮助!