Passing of data using Timer
suggest changeIf you want to pass some data with the timer trigger, you can do it with the userInfo parameter.
Here is a simple approach that shows how you can pass data to the triggered method from the timer. The timer keeps a strong reference to target until it is invalidated.
Swift 3
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector:#selector(iGotCall(sender:)), userInfo: ["Name": "i am iOS guy"], repeats:true)
Objective-C
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(iGotCall:)
userInfo:@"i am iOS guy" repeats:YES];
The Swift call passes ["Name": "i am iOS guy"]. The Objective-C call passes the string @"i am iOS guy". Force-unwrapping sender.userInfo crashes when userInfo is nil.
Swift 3
func iGotCall(sender: Timer) {
print((sender.userInfo)!)
}
Objective-C
- (void)iGotCall:(NSTimer*)theTimer {
NSLog (@"%@", (NSString*)[theTimer userInfo]);
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents