Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 898
Okhan Okbay Avatar asked Jul 11 '15 11:07

Okhan Okbay


1 Answers

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:
        print("Menu is opened")
    case .menuIsClosed:
        print("Menu is closed")
} 
like image 198
Alladinian Avatar answered Sep 21 '22 01:09

Alladinian