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中的开关错误?- “切换必须是详尽无遗的,考虑添加一个默认子句."
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01