2015-04-16 26 views
0

此代碼在計數器Label.text變量的第二個+符號上拋出錯誤「Binary Operator'+'不能應用於'String'和'Double'類型的操作數。如果我在計數器Label.text中的minutesLabel後面刪除所有內容,則錯誤將消失(這是用Swift編寫的)。也對任何格式混淆抱歉我是第一次使用。爲什麼我的代碼在counterLabel.text變量的第二個+號上拋出一個二元運算符錯誤?

func updateCounter(timer: NSTimer) { 

     let hours = floor(stopWatchTime/pow(60, 2)) 
     let hoursInSeconds = hours * pow(60, 2) 

     let minutes = floor((stopWatchTime - hoursInSeconds)/60) 
     let minutesInSeconds = minutes * 60 

     let seconds = floor((stopWatchTime - hoursInSeconds - minutesInSeconds)/60) 
     let secondsInCentiseconds = seconds * 100 

     let centiseconds = stopWatchTime - hoursInSeconds - minutesInSeconds - secondsInCentiseconds 

     let hoursLabel = String(format: "%02.0f:", hours) 
     let minutesLabel = String(format: "%02.0f:", minutes) 
     let secondsLabel = String(format: "%02.0f:", seconds) 
     let centisecondsLabel = String(format: "%02.0f", centiseconds) 

     counterLabel.text = hoursLabel + minutesLabel + secondsLabel + centiseconds 

     stopWatchTime = stopWatchTime + 1 

回答

0

你忘了標籤添加到變量centiseconds,因爲它不是字符串,centisecondsLabelString。那就是爲什麼你得到了二元運算錯誤。 使用下面的代碼:

counterLabel.text = hoursLabel + minutesLabel + secondsLabel + centisecondsLabel 

,將解決這個問題!

相關問題