user info in NSTimer

Discussion in 'iOS Programming' started by grandM, Nov 6, 2016.

  1. grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #1
    Could someone explain me what user info is used for within the NSTimer class? The apple documentation isn't revealing that much.

    Thanks again!
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    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.
     
  3. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #3
    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?
     
  4. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #4
    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).
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    @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.
     
  6. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #6
    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.
     
  7. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #7
    interesting
    could you illustrate this with an example?
     
  8. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #8
    No. Creating an example should be an exercise for the student. Come back if there are bugs in your attempt.
     

Share This Page