本文介绍了swift中的开关错误?- “切换必须是详尽无遗的,考虑添加一个默认子句."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很肯定下面的 swift 代码已经涵盖了所有可能性,但 Xcode 一直告诉我,切换必须是详尽的,考虑添加一个默认子句."
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中的开关错误?- “切换必须是详尽无遗的,考虑添加一个默认子句."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!