0

-[NSInvocationOperation initWithTarget:selector:object:] only accepts one object to be passed as an argument for the method that will be called. I want to use two arguments; how can I do that?

This is my code:

- (void)loadImage:(NSURL *)imageURL
{
    NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc]
                                        initWithTarget:self
                                        selector:@selector(requestRemoteImage:)
                                        object:imageURL];
    [queue addOperation:operation];
}

- (void)requestRemoteImage:(NSURL *)imageURL
{
    NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
    UIImage *image = [[UIImage alloc] initWithData:imageData];

    [self performSelectorOnMainThread:@selector(placeImageInUI:) withObject:image waitUntilDone:YES];
}
jscs
  • 63,694
  • 13
  • 151
  • 195
Khalid
  • 33
  • 1
  • 4
  • 1
    @H2CO3's answer is exactly correct, but I would strongly discourage using `NSInvocationOperation` unless you have a strong need for it. `NSBlockOperation` is much faster to generate, easier to code, and more flexible. Invocations are extremely expensive to create, and have been widely replaced with blocks. – Rob Napier Aug 28 '12 at 17:28

3 Answers3

1

You can either use an NSInvocation object to initialize the opertation:

- (id)initWithInvocation:(NSInvocation *)inv

(NSInvocation can handle multiple arguments); or you can wrap the needed arguments to an NSArray or NSDictionary, provided they're objects.

1

Ask the operation for its invocation and modify that before adding the operation to the queue.

 NSInvocation * invocation = [operation invocation];
 [invocation setArgument:&anotherObject atIndex:3];    
 // Indexes 0 and 1 are self and cmd, and you've already used index 2

Or, as Rob Napier suggested, use NSBlockOperation, which is far more flexible and easy to use.

Community
  • 1
  • 1
jscs
  • 63,694
  • 13
  • 151
  • 195
-1

you could send a dictionary with all the values you want

Ismael
  • 3,927
  • 3
  • 15
  • 23