0
When can the "default" be ommited in a Switch statement?
In the "Conditional Statement" part of the tutorial it shows this piece of code to explain the "where" command: let myPoint = (1, -1) switch myPoint { case let (x, y) where x == y: print("(\(x), \(y)) is on the line x == y") case let (x, y) where x == -y: print("(\(x), \(y)) is on the line x == -y") case let (x, y): print("(\(x), \(y)) is just some arbitrary point") } It has no "default" case. If I change the pairs of values for an Int: let Input = 500 switch Input { case let (x) where x <= 50: print ("in case a") case let (x) where x > 50: print ("in case b") } It gives me an error asking for a default value... Can someone please explain me why is that?
3 ответов
0
the default statement can be omitted only if all the possible cases are covered in the switch...case statement, otherwise it is always good to put a default statement to be executed in case the user inputs are not covered by any of the other case statement s
0
Hi Ramzi, thanks for your answer.
But I still don't see, why I'm getting the error in this code:
let Input = 500
switch Input {
case let (x) where x <= 50:
print ("in case a")
case let (x) where x > 50:
print ("in case b")
}
I mean, all possible cases are consider in these two case, right? In that case, why is still no ok?
Thanks in advance.
0
I haven't never programmed with swift before but reviewing your code I think that you should add another line :
case let (x):
print ("not in any case")
hope it helps
thanks