I’ve read around SO as well as looked at the Apple documentation for the NSTimer class, and it seems like the easiest way to create a timer is by using the following method:
[NSTimer scheduledTimerWithTimeInterval:15.0
target:self
selector:@selector(fireThisMethod:)
userInfo:nil
repeats:NO];
I understand that this will create a timer that fires after 15 seconds. The method that will be fired will be whatever method is passed into the “selector” (in this case “fireThisMethod”). Since the “repeats” is “NO,” this means that it will only be fired once. My questions are:
1) Is it possible to pass in a method to “selector” that has more than one parameter? I’ve seen methods without any parameters, or in this case 1 (since a “:” is present after the method name). A method such as fireThisMethod:anotherParameter:?
2) I’m not sure if I truly understand what the “target” and “userInfo” parameters are for. Will you ever have a “target” that isn’t “self?” Will you have a “userInfo” that isn’t nil? If so, in what scenarios?
Thanks
Question 1
No. Take a look in the docs. Quoting:
Selectors for methods without arguments are wrong. The argument to be passed is
the timer, so you can do things like distinguishing which timer fired from you
method (in case more than one timer calls the same method).
Question 2a
The target can be any object that implements the given method. Sometimes you may
want a specific object (other than self) to do the task after the timer fires.
Question 2b
This is exactly what you’re looking for.
userInfocan be any object, maybe adata container, a NSValue, or any other thing. Use this to pass extra
information to your method which will extract it as follows: