0
我一直試圖讓這個工作,但我得到一個錯誤代碼:線程1:信號SIGABRT在AppDelegate.swift。我想要做的是:創建儘可能準確的遊戲計時器,該計時器將在特定的日期和時間開始。我想使用viewWillLoad(),但我認爲runloop會初始化NSTimer。我嘗試了很多不同的方法來處理這個問題,如果有更好的方法,請告訴我。將不勝感激任何幫助!錯誤代碼:線程1:信號SIGABRT遊戲計時器與NSTimer和NSDate
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var gameTimerLabel: UILabel! // Game timer label, should be updated with NSTimer
func updateLabel()-> (String) {
let startDateAsString = "Mon, 12 Apr 2016 12:00:00 MDT" // Start date as a string
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
let startDate = dateFormatter.dateFromString(startDateAsString) // Start date as NSDate
let now = NSDate()
let difference = now.timeIntervalSinceDate(startDate!) // Time since start date
let dateComponentsFormatter = NSDateComponentsFormatter() // Format so time is in Min:Sec
let timer = dateComponentsFormatter.stringFromTimeInterval(difference)
self.gameTimerLabel.text = timer! // Updating Label
return timer! // Tried to use the return value to update timer
}
// Timer set up, should fire updateLabel each time
func timerSetup(){
let myTimer = NSTimer(timeInterval: 0.5, target: self, selector: "updateLabel", userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(myTimer, forMode: NSRunLoopCommonModes)
}
}
什麼是確切的崩潰日誌你得到?你從哪裏調用timerSetup方法?從updateLabel函數返回String的目的是什麼? –
您是否嘗試添加異常斷點以獲取有關崩潰原因的更多信息? –
@MidhunMP我得到另一個文件,默認AppDelegate.swift的崩潰日誌,我沒有改變它。我需要調用timerSetup,你是正確的,當我打電話時,我得到的錯誤代碼「在參數#1中缺少參數,我正在返回字符串作爲定時器來更新標籤,我可以將其刪除 –