我想在每次定時器觸發時更新選擇器函數中定時器的userInfo。在Swift中更改定時器選擇器函數中的userInfo
USERINFO:
var timerDic = ["count": 0]
定時器:
Init: let timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("cont_read_USB:"), userInfo: timerDic, repeats: true)
選擇功能:
public func cont_read_USB(timer: NSTimer)
{
if var count = timer.userInfo?["count"] as? Int
{
count = count + 1
timer.userInfo["count"] = count
}
}
我上最後一行的錯誤:
'AnyObject?'沒有名爲'下標'的成員
這裏有什麼問題? 在Objective_C這個任務有NSMutableDictionary
作爲userInfo
這不適合我。 – 2018-02-26 05:56:09
@AdrianBartholomew,我剛剛在一個Xcode 9.2(最新版)和Swift 4的應用程序中嘗試過這個工作。我不得不使用'Timer'而不是'NSTimer'並將'@ objc'添加到定時器更新函數中,但它工作正常。你看到了什麼症狀? – vacawama 2018-02-26 11:50:30