Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch error: Expected member name or constructor call - what's wrong?

Tags:

enums

ios

swift

I'd like to make a switch on my 3 cases, but I'm getting an error I can't solve:

Error: Expected member name or constructor call after type name

There must be something I've overlooked since I've been already using similar code. But now I have almost an empty project and can't figure out what's wrong?

import UIKit

class ViewController: UIViewController {

enum MyStateStatus {
    case Ready
    case Running
    case Stopped
}

@IBAction func actionPressed(sender: UIButton) {

    switch MyStateStatus {
    case MyStateStatus.Ready:
        print("I'm ready")
    default:
        print("other")
    }
}

I'm using Swift, Xcode 6.3.2

UPDATE: Replaced println() with print() for Swift 2.2 and Xcode 7 compatibility.

like image 875
Andrej Avatar asked Jun 29 '15 10:06

Andrej


1 Answers

In your example your are applying the switch to the enum declaration itself, but you have to switch over an object that contains one of the possible enum values.

For example:

var currentState: MyStateStatus = .Ready

@IBAction func actionPressed(sender: UIButton) {
    switch currentState {
    case .Ready:
        println("I'm ready")
    default:
        println("other")
    }
}
like image 89
Eric Aya Avatar answered Sep 30 '22 18:09

Eric Aya