iOS Passing of data using Timer


Example

If you you want to pass some data with the timer trigger you can do it with the userInfoparameter.

Here is the simple approach that gives brief idea about how you can pass the data to triggered method from the Timer.

[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 above line of code passing ["Name": "i am iOS guy"] into the userInfo. So now when the iGotCall get call you can get the passed value as below code snippet.

[Swift 3]

func iGotCall(sender: Timer) {
        print((sender.userInfo)!)
    }

[Objective - C]

- (void)iGotCall:(NSTimer*)theTimer {
    NSLog (@"%@", (NSString*)[theTimer userInfo]);
}