1

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?

4

2 回答 2

4

Check the docs for dispatch_after().

于 2012-07-24T02:44:13.743 回答
2

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.
于 2012-08-06T22:48:19.543 回答