2015-06-30 33 views
0

我正在嘗試將舊的Objective C項目轉換爲Swift,以便在我計劃發佈的應用程序中使用我無法擺脫這個小問題。我有各種各樣的,我的「TimeViewController從拉動對象 - 文件,這是我在看:枚舉案例模式不能匹配非枚舉類型'TabataStates'(Obj-C到Swift轉換)的值

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庫時究竟做了什麼錯誤,因此我非常感謝任何輸入,因爲我對開關盒沒有太多的瞭解。

回答

3

您需要使用NS_ENUM宏來定義您的枚舉,因此它們將在Swift中可用。這裏有一個如何枚舉應改爲:

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

對不起,一個奇怪的命名,它只是保持它兼容與您當前密碼的原因。

+0

那麼我將如何在我的特定情況下實現這一點? –

+0

@JohnHarding我更新了一個實際的代碼示例。應該放棄替代您的枚舉定義。除枚舉之外的其他東西都需要更改。 –

+0

謝謝你的幫助! –