developthat developthat - 6 months ago 120
Swift Question

Switch condition evaluates to a constant in Swift

I have this code

import UIKit

enum menuSituation{
case menuIsOpened
case menuIsClosed

}

class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

var currentSituation = menuSituation.menuIsClosed

switch(currentSituation){ //Here is the warning
case .menuIsOpened:
println("Menu is opened")
break
case .menuIsClosed:
println("Menu is closed")
break

}
}


In the line where I start to define switch statement, it gives me the warning :


Switch condition evaluates to a constant


How can I get rid of this warning?

Answer

Well, it basically means that the switch will always evaluate to menuIsClosed. You probably meant something like this:

var currentSituation = aSituation // That would be a menuSituation (known at runtime)

// Also note that 'break' is not needed in (non-empty) match cases
switch currentSituation {
    case .menuIsOpened:
        println("Menu is opened")
    case .menuIsClosed:
         println("Menu is closed")
} 
Comments