简体   繁体   中英

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");
}

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];

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM