简体   繁体   中英

Enum case pattern cannot match values of the non-enum type 'TabataStates' (Obj-C to Swift Conversion)

I am attempting to convert an older Objective C Project into Swift for use in an application that I plan to release, yet I am unable to get past this little issue. I have a variety of Obj-c files that my "TimeViewController is pulling from, and this is what I am looking at:

typedef enum {
IDLE,
STARTING,
EXERCISE,
RELAXATION
} TabataStates;

and

// Actions

- (TabataStates)getState;

and

- (void)update {
switch (tabataState) {
    case IDLE:
        currentRound = 0;
        currentTime = startingDuration;
        [tabata setState:STARTING];
        break;
    case STARTING:
        currentTime -= UPDATE_INTERVAL;
        if ((currentTime > 0.9 && currentTime < 1.1) || (currentTime > 1.9 && currentTime < 2.1)) {
            [[NSNotificationCenter defaultCenter] postNotificationName:PrepareSignal object:self];
        }
        if (currentTime <= 0) {
            currentTime = exerciseDuration;
            ++currentRound;
            [tabata setState:EXERCISE];
        }
        break;
    case EXERCISE:
        currentTime -= UPDATE_INTERVAL;
        if (currentTime <= 0) {
            if (currentRound >= roundAmount) {
                [self stop];
            }
            else {
                currentTime = relaxationDuration;
                [tabata setState:RELAXATION];
            }
        }
        break;
    case RELAXATION:
        currentTime -= UPDATE_INTERVAL;
        if (currentTime <= 0) {
            currentTime = exerciseDuration;
            ++currentRound;
            [tabata setState:EXERCISE];
        }
        break;
    default:
        break;
}
[[NSNotificationCenter defaultCenter] postNotificationName:TimerUpdated object:self];
}

and

- (void)tabataStateChanged:(NSNotification *)notification {
switch ([tabata getState]) {
    case STARTING:
        self.timerLabel.textColor = [theme getColorFor:THEME_TIMER_INACTIVE];
        break;
    case EXERCISE:
        self.timerLabel.textColor = [theme getColorFor:THEME_TIMER_EXERCISE];
        break;
    case RELAXATION:
        self.timerLabel.textColor = [theme getColorFor:THEME_TIMER_RELAXATION];
        break;
    default:
        self.timerLabel.textColor = [theme getColorFor:THEME_TIMER_INACTIVE];
        break;
}
[self showRound];
[self tabataTimerUpdated:notification];
}

In my "TimeViewController" (swift), I have what I am trying to convert to, which is presenting me with the error "Enum case pattern cannot match values of the non-enum type 'TabataStates'":

func tabataStateChanged(NSNotification) {
    switch tabata.getState() {

    case .STARTING: //error here
        self.timerLabel.textColor = theme.getColorFor(THEME_TIMER_INACTIVE)
        break
    case .EXERCISE: //error here
        self.timerLabel.textColor = theme.getColorFor(THEME_TIMER_EXERCISE)
        break
     case .RELAXATION:  //error here
        self.timerLabel.textColor = theme.getColorFor(THEME_TIMER_RELAXATION)
        break
    default:
        self.timerLabel.textColor = theme.getColorFor(THEME_TIMER_INACTIVE)
        break
    }

    self.showRound()
    self.tabataTimerUpdated(NSNotification)

}

I am not sure what exactly I have done wrong in my attempt to re-use some Obj-C library's, so I would appreciate any input, as I do not have a ton of knowledge regarding switch cases.

You need to use NS_ENUM macro to define your enums, so they will be available in Swift. Here is a how you enum should be changed:

typedef NS_ENUM(NSInteger, TabataStates) {
   TabataStatesIDLE
   TabataStatesSTARTING,
   TabataStatesEXERCISe,
   TabataStatesRELAXATION
};

Sorry for a strange naming, it is just for the reason of keeping it compatible with your current code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM