Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
958 views
in Technique[技术] by (71.8m points)

a switch bug in swift? - "Switch must be exhaustive, consider adding a default clause."

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...