2014-12-28 26 views
-2

我正在學習該語言,並且正在嘗試製作秒錶和倒數計時器應用程序。 我已經做了所有的秒錶,但倒數計時器給我麻煩。「字符串」與「Int8」不相同 - SWIFT

我有一個選取器視圖來將標籤設置爲應從其倒數的位置。那是我遇到麻煩的地方。我似乎無法弄清楚如何讓NSTimer從用戶從Picker View中挑選的東西倒計時。

//UIPickerView 

var numbers = ["1.00", "2.00", "3.00", "4.00", "5.00", "6.00", "7.00","8.00", "9.00", "10.00", "11.00","12.00","13.00", "14.00", "15.00", "16.00", "17.00", "18.00", "19.00", "20.00"] 


func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { 
    return 1 
} 

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
    return numbers.count 
} 

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { 
    return numbers[row] 
} 

func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int){ 

    var displayTime = "\(numbers[row])" 
    timerDisplay.text = String(displayTime--) **LINE THAT GIVES ERROR** 
    timeInMilliseconds -= 0.01   
} 

這是我對拾取器視圖所擁有的。如果您需要查看更多代碼來幫助我,請告訴我。 任何幫助表示讚賞。 謝謝。

+0

哪條線是給你的錯誤? –

+0

我編輯了原文。我添加了哪一行給出錯誤。 –

+1

'displayTime'已經是一個字符串,所以「 - 」沒有多大意義。 (另外,'「\(numbers [row])」'與'numbers [row]'...兩個字符串幾乎相同。) –

回答

0

如果要在文本中顯示「.00」部分,則需要提取整個數字以進行算術運算。

喜歡的東西:

var text = "" 
var whole: Int? = nil 
if let index = numbers[row].rangeOfString(".") { 
    whole = numbers[row].substringToIndex(index.startIndex).toInt() 
} 
if whole != nil { 
    text = "\(whole! - 1).00" 
} 
+0

非常感謝。我是否將該代碼塊放入didSelectRow函數中? @PhillipMills –