I want to update the userInfo of the timer in the selector function every time the timer fires.
userInfo:
var timerDic = ["count": 0]
Timer:
Init: let timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("cont_read_USB:"), userInfo: timerDic, repeats: true)
selector function:
public func cont_read_USB(timer: NSTimer)
{
if var count = timer.userInfo?["count"] as? Int
{
count = count + 1
timer.userInfo["count"] = count
}
}
I get an error on the last line:
'AnyObject?' does not have a member named 'subscript'
What is wrong here?
In Objective_C this task worked with a NSMutableDictionary
as userInfo
NSTimer.userInfo
is of type AnyObject
so you need to cast it to your target object:
public func cont_read_USB(timer: NSTimer)
{
if var td = timer.userInfo as? Dictionary<String,Int> {
if var count = td["count"] {
count += 1
td["count"] = count
}
}
}