Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

grandM

macrumors 68000
Original poster
Oct 14, 2013
1,520
302
Could someone explain me what user info is used for within the NSTimer class? The apple documentation isn't revealing that much.

Thanks again!
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
userInfo is for the user to decide. The user in this case is the developer. An example might be a timer that's meant to go off at a set of times that are not regular, say 1 sec, then 4 secs, then 10 secs. The userInfo could be an array holding those times and each time the timer goes off it sets the next time to go off from the list. When the timer is first created the array is created and the userInfo is set to the timer. Then the array could have one value removed. Then a new timer is created with the new array. And so on.

In most cases there are other ways to accomplish things that the userInfo might be used for but sometimes it's the most convenient way. It's kind of like the userInfo in NSNotification and NSError. Use it for whatever you want.
 
  • Like
Reactions: grandM

grandM

macrumors 68000
Original poster
Oct 14, 2013
1,520
302
userInfo is for the user to decide. The user in this case is the developer. An example might be a timer that's meant to go off at a set of times that are not regular, say 1 sec, then 4 secs, then 10 secs. The userInfo could be an array holding those times and each time the timer goes off it sets the next time to go off from the list. When the timer is first created the array is created and the userInfo is set to the timer. Then the array could have one value removed. Then a new timer is created with the new array. And so on.

In most cases there are other ways to accomplish things that the userInfo might be used for but sometimes it's the most convenient way. It's kind of like the userInfo in NSNotification and NSError. Use it for whatever you want.
So if you initialize a Timer object myTime and you add as a parameter [1.0, 4.0, 10.0] it will fire at the first, fourth and tenth second? Why is the creation of a new Timer object required then?
 

firewood

macrumors G3
Jul 29, 2003
8,141
1,384
Silicon Valley
User info is just a way to pass information to the timer callback function (for instance, the object creating the timer, or a sound, or anything known at the time of creating the timer, but possibly not at other times). The information must be in the form of an Objective C object (perhaps encapsulating lots of other information).
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
@gramdM, my example was just an example. There is no automatic firing of timers at staggered intervals. If you want that you could use my example description to do that.

OK, another, maybe simpler example. You want an image to appear in an image view after a delay. Or you want a sound to play, after a delay. You create the timer object and set the userInfo property to be the UIImage or Sound object to be used. Then when the timer fires the timer callback method reads the userInfo and casts it to the appropriate type and then uses it, to show the image or play the sound.
 
  • Like
Reactions: grandM

AxoNeuron

macrumors 65816
Apr 22, 2012
1,251
855
The Left Coast
The userInfo property of the timer can be used and abused however you like. Basically it exists for people who are reluctant to use global scope variables but want to persist some value/object for some period of time.

It's especially useful when you have two separate timers that trigger/act upon the same function to accomplish different tasks. Thus, the timer function will exhibit either completely or partially different behavior depending on what was passed in via user info.
 

grandM

macrumors 68000
Original poster
Oct 14, 2013
1,520
302
The userInfo property of the timer can be used and abused however you like. Basically it exists for people who are reluctant to use global scope variables but want to persist some value/object for some period of time.

It's especially useful when you have two separate timers that trigger/act upon the same function to accomplish different tasks. Thus, the timer function will exhibit either completely or partially different behavior depending on what was passed in via user info.
interesting
could you illustrate this with an example?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.