我正试图将一个较旧的Objective项目转换为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“(TabataStates)中,我有我想要转换的东西,它显示了错误"Enum模式不能匹配非enum类型‘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库的,所以我会感谢任何输入,因为我没有太多的知识有关开关的情况。
发布于 2015-06-30 22:47:44
您需要使用NS_ENUM宏来定义枚举,这样它们就可以在Swift中使用。下面是应该如何更改您的枚举:
typedef NS_ENUM(NSInteger, TabataStates) {
TabataStatesIDLE
TabataStatesSTARTING,
TabataStatesEXERCISe,
TabataStatesRELAXATION
};很抱歉出现了一个奇怪的命名,这只是为了保持它与您当前代码的兼容性。
https://stackoverflow.com/questions/31149732
复制相似问题