Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum case '...' is not a member of type '...'

Tags:

ios

swift

I have an enum:

enum State {
    case FullOpen
    case HalfOpen
    case Closed
}

and this code:

var stateForConversionView: State!
...


var previousState: State!

if true {
    previousState = stateForConversionView!
    switch previousState {
    case .FullOpen:
        stateForConversionView = .HalfOpen
    case .HalfOpen:
        stateForConversionView = .FullOpen
    case .Closed:
        stateForConversionView = .HalfOpen
    default:
        break
    }
}

I got an error on each switch statement:

Enum case 'FullOpen' not found in type 'State!'
Enum case 'HalfOpen' not found in type 'State!'
Enum case 'Closed' not found in type 'State!'

I don't really understand why. Can someone explain me please?

like image 919
Nico Avatar asked Jun 27 '15 06:06

Nico


2 Answers

This way It will work fine :

if true {
    previousState = stateForConversionView

    switch previousState! {
    case .FullOpen:
        stateForConversionView = .HalfOpen
    case .HalfOpen:
        stateForConversionView = .FullOpen
    case .Closed:
        stateForConversionView = .HalfOpen
    default:
        break
    }
}

You need to add !.

For more info refer THIS.

like image 53
Dharmesh Kheni Avatar answered Nov 15 '22 12:11

Dharmesh Kheni


If the condition variable is in a different type of the "State". You should use rawValue property.

var previousState:String
previousState = stateForConversionView
 switch previousState {
        case State.FullOpen.rawValue:
            stateForConversionView = .HalfOpen
        case State.HalfOpen.rawValue:
            stateForConversionView = .FullOpen
        case State.Closed.rawValue:
            stateForConversionView = .HalfOpen
        default:break

        }
like image 2
Ahmed Lotfy Avatar answered Nov 15 '22 13:11

Ahmed Lotfy