Chris
Chris

Reputation: 67

How can I use Int.min and Int.max on a switch statement?

enter image description here I'm very new to coding on swift < 1 year... So, I accept I need help... In this exercise an error message pops up saying 'expected ':' after case'

my code here:

let Temperature = 65
switch Temperature {

case Int.min <.. 65:
print(“the temperature is too low”)

case 65...75:
print(“the temperature is perfect”)

case 75>..Int.max:
print(“the temperature is too high”)

default:
print (“please set a temperature value”)

Upvotes: 2

Views: 810

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236458

Note that there is no ranges like <.., >.. or ..>, you can only use CountableClosedRange ... or CountableRange ..< and if you are coding in Swift 4 or later there is no need to use Int.min or Int.max you can simply omit it and use partial range operators.

switch Temperature {
case ..<65:
    print("the temperature is too low")
case 65...75:
    print("the temperature is perfect")
case 75...:
    print("the temperature is too high")
default:
    break
}

If you are using Swift 3 or earlier you can do as follow:

switch Temperature {
case .min ..< 65:
    print("the temperature is too low")
case 65 ... 75:
    print("the temperature is perfect")
case 75 ... .max:
    print("the temperature is too high")
default:
    break
}

Upvotes: 3

Related Questions