枚举大小写模式无法匹配非枚举类型 'TabataStates' 的值(Obj-C 到 Swift 的转换)
Enum case pattern cannot match values of the non-enum type 'TabataStates' (Obj-C to Swift Conversion)
我正在尝试将一个较旧的 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) 中,我有我要转换成的内容,它向我显示错误 "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)
}
我不确定我在尝试重新使用某些 Obj-C 库时到底做错了什么,所以我将不胜感激任何输入,因为我对 switch cases 没有太多的了解。
您需要使用 NS_ENUM 宏来定义您的枚举,因此它们将在 Swift 中可用。这是枚举的更改方式:
typedef NS_ENUM(NSInteger, TabataStates) {
TabataStatesIDLE
TabataStatesSTARTING,
TabataStatesEXERCISe,
TabataStatesRELAXATION
};
对不起,一个奇怪的命名,这只是为了保持它与您当前的代码兼容。
我正在尝试将一个较旧的 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) 中,我有我要转换成的内容,它向我显示错误 "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)
}
我不确定我在尝试重新使用某些 Obj-C 库时到底做错了什么,所以我将不胜感激任何输入,因为我对 switch cases 没有太多的了解。
您需要使用 NS_ENUM 宏来定义您的枚举,因此它们将在 Swift 中可用。这是枚举的更改方式:
typedef NS_ENUM(NSInteger, TabataStates) {
TabataStatesIDLE
TabataStatesSTARTING,
TabataStatesEXERCISe,
TabataStatesRELAXATION
};
对不起,一个奇怪的命名,这只是为了保持它与您当前的代码兼容。