Roboboy64
Roboboy64

Reputation: 13

Method After Delay with Parameters in Objective-C

I'm fairly new to Objective-C, so forgive me if I'm missing something common. I have a method with several (four) required parameters that needs to run over and over with a delay between runs. Normally, I'd use:

[self performSelector:@selector(methodName:) withObject:nil afterDelay:1.0f/10f];

The problem is I need the method to pass parameters (more than one) back into itself after the delay; but this bit of code can only pass one over. Is there something I'm missing here?

Upvotes: 1

Views: 1274

Answers (2)

user102008
user102008

Reputation: 31323

To answer the actual question, there are generally two ways to use performSelector:withObject:afterDelay: with multiple pieces of data:

  1. Change the method to take only one parameter, usually by packing the multiple items into a collection like an array. The caller will have to pack and the callee will have to unpack the items. You can add a wrapper method if you don't want to modify the original method.
  2. Use NSInvocation to represent the call of the method with multiple parameters, then do performSelector:withObject:afterDelay: on the invocation's invoke method. This method does not require changing any method parameters or adding any methods, but is more verbose.

Upvotes: 2

NSResponder
NSResponder

Reputation: 16861

Check the docs for dispatch_after().

Upvotes: 4

Related Questions