Captain Buck
Captain Buck

Reputation: 689

How to pass object in NSInvocationOpreation

I have created an NSInvocationOpertion object as follows

NSString *myString = @"Jai Hanuman";
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(taskMethod) object:myString];
NSOperationQueue *operatioQueue = [[NSOperationQueue alloc] init];
    [operatioQueue addOperation:operation];

and can anybody tell me how can I access the myString object in taskMethod? Is it possible?

- (void)taskMethod{
    NSLog(@"Oh! Invocation's working buck");
}

Upvotes: 0

Views: 35

Answers (2)

Teja Nandamuri
Teja Nandamuri

Reputation: 11201

You can try this:

Change your method to:

- (void)taskMethod:(NSString *) message{
NSLog(@"Oh! Invocation's working buck");
        message=//your string object;
  }

and

NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(taskMethod:) object:myString];

Upvotes: 1

Martin R
Martin R

Reputation: 539685

Define the method with one parameter:

- (void)taskMethod: (NSString *)theString {
    NSLog(@"Called with string: %@", theString);
}

and create the operation as:

NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self 
         selector:@selector(taskMethod:) object:myString];

Note the additional colon in the selector, it indicates that the method takes one argument.

Upvotes: 1

Related Questions