swift中的开关错误?- “开关必须详尽无遗,考虑添加一个默认条款." [英] a switch bug in swift? - "Switch must be exhaustive, consider adding a default clause."
本文介绍了swift中的开关错误?- “开关必须详尽无遗,考虑添加一个默认条款."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我确信以下 swift 代码已经涵盖了所有可能性,但 Xcode 一直告诉我,Switch 必须详尽无遗,考虑添加一个 default 子句."
I am positive that the following swift code has covered all possibilities, but Xcode keeps telling me that, "Switch must be exhaustive, consider adding a default clause."
谁能告诉我我错过了什么?谢谢.
Can anyone tell me what did I miss? Thanks.
let a = false
let b = false
let c = false
func test(a: Bool, _ b: Bool, _ c: Bool) {
switch (a, b, c) {
case (true, false, _):
print("Moved left!!!")
case (true, true, _):
print("Moved right!!!")
case (false, _, false):
print("Moved up!!!")
case (false, _, true):
print("Moved down!!!")
// Error: Switch must be exhaustive, consider adding a default clause.
}
}
test(false, false, false)
test(false, false, true)
test(false, true, false)
test(false, true, true)
test(true, false, false)
test(true, false, true)
test(true, true, false)
test(true, true, true)
推荐答案
编译器无法总结您的模式,因为它对它来说太复杂或太不寻常.如果您的模式更规则,例如:
The compiler is unable to conclude about your pattern because it is too complex or too unusual for it. If your pattern would have been more regular like:
case (true, false, _):
print("Moved left!!!")
case (true, true, _):
print("Moved right!!!")
case (false, false, _):
print("Moved up!!!")
case (false, true, _):
print("Moved down!!!")
那么编译器就不会抱怨了.在这种情况下,很容易得出结论,您涵盖了所有情况.
then the compiler would have not complained. It that case it is easy for it to conclude that you covered all the cases.
这篇关于swift中的开关错误?- “开关必须详尽无遗,考虑添加一个默认条款."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文