我想在每次定时器触发时更新选择器函数中定时器的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