views:

709

answers:

3

I have an NSTimer which is setup like this:

[NSTimer scheduledTimerWithTimeInterval:0.5 target:timerTarget selector:NSSelectorFromString(targetSelector) userInfo:nil repeats:YES];

How can I get the return value of the timerTarget method?

+4  A: 

What are you doing with the return value? The target is run asynchronously 0.5 seconds later. The call to scheduledTimerWithTimeInterval:target:selector:useInfo:repeats: returns instantaneously, at which point the target has not been run. If you really need the return value right now, then don't bother with a timer and just call the selector normally.

Adam Rosenfield
I need the return value every .5 seconds. The target is monitoring a value that changes often and it returns that value so that I can see if a change actually occurred.
macinjosh
Then you should act on the change in the timer method. Remember, scheduledTimerWithTimeInterval::::: only returns *once*. It doesn't return once for every call to the timer. You call scheduledTimer…::::: once, so it returns once.
Peter Hosey
+1  A: 

Following up on Adam's good answer, if you feel you need the return value of the targeted method after it finishes running, then what you probably want to do is have the targeted method itself perpetuate some further action.

For instance, you might use the "userInfo" parameter to pass along a reference to the interested object (self?). The targeted method could then, instead of returning a value, send the updated value to self with a pre-arranged selector. For instance in your target method, you could end with something like:

[[theTimer userInfo] setWhatever:blah];

Conceptually you need to take Adam's advice and consider the fact that the method being called by the timer is happening in the future and if you want to connect back to a specific class and its workflow, you need to connect it up somehow yourself.

danielpunkass
+3  A: 

You can't, because there is no return value.

Quoth the documentation of the scheduledTimerWithTimeInterval::::: method:

The selector must have the following signature:

- (void)timerFireMethod:(NSTimer*)theTimer

Now, you could declare your method differently and then return a value in it, but then your method has a different signature than NSTimer expects. That'll work fine now, but is not guaranteed to work in the future—it may break at any time, especially a change in the runtime.

More to the point, since NSTimer is not expecting a return value, it has no provisions to receive one and pass it off anywhere.

Moreover, as I said in my comment on Adam's answer, your call to scheduledTimerWithTimeInterval::::: only returns once, and (as Adam said) it does so immediately. You don't get one return value every 0.5 seconds, because you don't get one return every 0.5 seconds—you get one return only (per timer).

Whatever information you're finding out in your timer method, you need to either act on it there or send a message with the information from there to another method.

Peter Hosey