Thursday, 15 September 2011

Change userInfo in timer selector function in Swift -



Change userInfo in timer selector function in Swift -

i want update userinfo of timer in selector function every time timer fires.

userinfo:

var timerdic = ["count": 0]

timer:

init: allow 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 error on lastly line:

'anyobject?' not have fellow member named 'subscript'

what wrong here? in objective_c task worked nsmutabledictionary userinfo

to create work, declare timerdic nsmutabledictionary:

var timerdic:nsmutabledictionary = ["count": 0]

then in cont_read_usb function:

if allow timerdic = timer.userinfo as? nsmutabledictionary { if allow count = timerdic["count"] as? int { timerdic["count"] = count + 1 } }

discussion:

swift dictionaries value types, if want able update have pass object. using nsmutabledictionary object type passed reference, , can modified since mutable dictionary.

swift timer userinfo

No comments:

Post a Comment