a switch bug in swift? - quot;Switch must be exhaustive, consider adding a default clause.quot;(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中的开关错误?- “切换必须是详尽无遗的,考虑添加一个默认子句."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:swift中的开关错误?- “切换必须是详尽无遗的,考虑添加一个默认子句."


- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- URL编码Swift iOS 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- UITextView 内容插图 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01