我試圖找出我怎麼能寫爲每15秒將「1」的整數轉換爲變量的函數。所以每次15秒通行證:myVar的:INT + = 1如何計時功能添加到一個變量迅速
我試圖設置一個計時器:
myTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(playFunc), userInfo: nil, repeats: true)
我試圖找出我怎麼能寫爲每15秒將「1」的整數轉換爲變量的函數。所以每次15秒通行證:myVar的:INT + = 1如何計時功能添加到一個變量迅速
我試圖設置一個計時器:
myTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(playFunc), userInfo: nil, repeats: true)
嘛,只是通過15.0
而不是1.0
到您的計時器調用是這樣的:
myTimer = NSTimer.scheduledTimerWithTimeInterval(15.0, target: self, selector: #selector(playFunc), userInfo: nil, repeats: true)
既然你在你的類具有可變myVar
地方
var myVar = 0 // or initialize it to whatever you like
你只需要實現playFunc
將由計時器每15秒被稱爲:
func playFunc() {
self.myVar += 1
}
你可以簡單地改變這樣的:
myTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(playFunc), userInfo: nil, repeats: true)
這樣:(更改1.0
到15.0
)
myTimer = NSTimer.scheduledTimerWithTimeInterval(15.0, target: self, selector: #selector(playFunc), userInfo: nil, repeats: true)
或者你可以這樣做:
var timesPlayed = 0
func playFunc() {
timesPlayed += 1
if timesPlayed % 15 == 0 {
myVar += 1
}
}
這樣做是每一個運行該功能時,它會嘗試看看是否可劃分15如果是這樣,則意味着該功能已運行15次,這將隨後加1到您的變量的時間。