A bit of confusion on my part


Hello. I need some help with understanding the concept with the Logger.

Every 2 seconds NSTimer calls the method sayOuch: on the target object called logger which in turn simply logs “Ouch!” to the console.

To make things run faster, @selector allows a lookup of the numerical representation of sayOuch:

in the Logger Class, I’m not understanding this line:

  1. Why does this method need the NSTimer argument?
  2. What’s the deal with the *)t portion? What about )*t? Where is the t pointer being used?


NSTimer expects a callback defined with that specific signature (a NSTimer object passed to the message) so it can pass itself.

If the invoked message needs to know more about the timer that invoked it, it has access to the object by way of the argument. If it doesn’t need to know that, it can simply disregard it.

If you check Apple’s documentation, it has more details: developer.apple.com/library/mac … Timer.html

Also, it sounds like you’re confused about the syntax, so in summary:
void <== Means the callback message won’t return anything
sayOuch: <== Message name
(NSTimer *) <== the sole argument will be (a pointer to) an object of type NSTimer
t <== Name of the variable the message can use to reference the NSTimer object (this can be any valid identifier, t is just a convenient short label)