swift中的开关错误?- “切换必须是详尽无遗的,考虑添加一个默认子句."

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
appearance().setBackgroundImage Not Working On Custom Class(外观().setBackoundImage在自定义类上不起作用)
Show/Hide barButtonItem(显示/隐藏barButtonItem)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)