2015-11-06 64 views
0

我試圖檢查數組中的多個值與參考值(倒數計時器)的關係。在swift中檢查數組中的多個值,然後根據匹配值的索引執行操作

例如:

X是時間值我想要做的,當X == timerInfo.timerCount, 同樣用於Y

我無法弄清楚如何檢查行動,如果任一值X或Y數組中的[X,Y]是==我timerInfo.timerCount值,如果是從timerInfo的該索引取數據!.alertInfo [X或Y]和做的東西與它

 if timerCounter - timerInfo!.alertInfo[0...timerInfo!.alertInfo.count-1] + 1 == timerInfo.timerCount { 
      AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) 

      //update text from value 
      alertInfo.text = "\(timerInfo!.alertInfo[0]!.alertText)" 
      self.view.backgroundColor = timerInfo!.alertInfo[0]!.alertColor 
     } 
+0

你可以使用NSArray並使用它的indexOfObject mehtod。或者你遍歷索引從0到count-1。 –

+0

好的非常感謝,我重複,只是檢查其工作,並會發布 –

回答

0
 for i in (0...timerInfo!.alertInfo.count-1) { 
      print("\(timerCounter!), \((timerInfo!.alertInfo[i]?.alertCounter)!) , \(timerInfo!.timerCount)") 
      if timerCounter! - (timerInfo!.alertInfo[i]?.alertCounter)! - 1 == (timerCounter! - timerInfo!.timerCount) { 

       AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) 
       alertInfo.text = "\(timerInfo!.alertInfo[i]!.alertText!)" 
       self.view.backgroundColor = timerInfo!.alertInfo[i]!.alertColor 
      } 
     } 
0
let arr = [1, 2, 3, 4] 
arr.contains(3) // true 
arr.contains(0) // false 
相關問題