我正在尝试将旧的Objective C项目转换为Swift,以便在我计划发布的应用程序中使用,但我无法解决这个小问题。我有各种各样的Obj-c文件,我的“TimeViewController”正在推出,这就是我所看到的:
typedef enum {
IDLE,
STARTING,
EXERCISE,
RELAXATION
} TabataStates;
和
// Actions
- (TabataStates)getState;
和
- (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];
}
和
- (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];
}
在我的“TimeViewController”(swift)中,我有我想要转换的内容,它向我显示错误“枚举案例模式无法匹配非枚举类型'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)
}
我不确定在尝试重新使用某些Obj-C库时我究竟做错了什么,所以我很感激任何输入,因为我对切换案例没有太多的了解。
答案 0 :(得分:4)
您需要使用NS_ENUM宏来定义您的枚举,因此它们将在Swift中可用。以下是您应该如何更改枚举的内容:
right:22px
对于一个奇怪的命名很抱歉,这只是为了保持它与您当前的代码兼容。