Andrej Andrej - 4 months ago 36
iOS Question

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

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.

Answer

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")
    }
}