Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exhaustive condition of switch case in Swift

Tags:

ios

swift

The Apple documentation says

Every switch statement must be exhaustive. That is, every possible value of the type being considered must be matched by one of the switch cases.

So in new Xcode I have placed a code like this

println(UInt16.min); // Output : '0' println(UInt16.max); // Output : '65535'  var quantity : UInt16 = 10;  switch quantity { case 0...65535: //OR case UInt16.min...UInt16.max:     println(); default:     println(); } 

Now if i remove the default section I get a compiler error:

Switch must be exhaustive
Do you want to add missing cases? Fix

So my question is for a case that I have mentioned as case 0...65535: have I not mentioned all the case values for an UInt16 ?? But still I am getting an error ?? Why am I getting this error, Did i miss something ??

like image 612
Girish Nair Avatar asked Nov 01 '14 05:11

Girish Nair


People also ask

What does switch must be exhaustive?

The section about control flow of the language guide says: Every switch statement must be exhaustive. That is, every possible value of the type being considered must be matched by one of the switch cases.

Can we use condition in switch case?

Switch Case In C In a switch statement, we pass a variable holding a value in the statement. If the condition is matching to the value, the code under the condition is executed. The condition is represented by a keyword case, followed by the value which can be a character or an integer. After this, there is a colon.

Does switch statement execute all cases?

A switch statement first evaluates its expression. It then looks for the first case clause whose expression evaluates to the same value as the result of the input expression (using the strict comparison, === ) and transfers control to that clause, executing all statements following that clause.

Does switch need break in Swift?

Although break isn't required in Swift, you can use a break statement to match and ignore a particular case or to break out of a matched case before that case has completed its execution. For details, see Break in a Switch Statement. The body of each case must contain at least one executable statement.

What is an exhaustive switch statement in Swift?

- Using Swift - Swift Forums The section about control flow of the language guide says: Every switch statement must be exhaustive. That is, every possible value of the type being considered must be matched by one of the switch cases. That s…

Is it better to break a switch statement in Swift?

This makes the switch statement safer and easier to use than the one in C and avoids executing more than one switch case by mistake. Although break isn’t required in Swift, you can use a break statement to match and ignore a particular case or to break out of a matched case before that case has completed its execution.

Should every switch statement be exhaustive?

Every switch statement must be exhaustive. That is, every possible value of the type being considered must be matched by one of the switch cases. If it’s not appropriate to provide a case for every possible value, you can define a default case to cover any values that aren’t addressed explicitly.

What is a switch control statement in Swift?

In this article, you will learn to use switch control statements to control the flow of your program's execution. The switch statement allows us to execute a block of code among many alternatives. The syntax of the switch statement in Swift is:


1 Answers

Swift only truly verifies that a switch block is exhaustive when working with enum types. Even a switching on Bool requires a default block in addition to true and false:

var b = true switch b { case true:  println("true") case false: println("false") } // error: switch must be exhaustive, consider adding a default clause 

With an enum, however, the compiler is happy to only look at the two cases:

enum MyBool {     case True     case False }  var b = MyBool.True switch b { case .True:  println("true") case .False: println("false") } 

If you need to include a default block for the compiler's sake but don't have anything for it to do, the break keyword comes in handy:

var b = true switch b { case true:  println("true") case false: println("false") default: break } 
like image 136
Nate Cook Avatar answered Sep 24 '22 13:09

Nate Cook