NSInvocationOperation - selector with multiple arguments

442 views
Skip to first unread message

Jeremy Greenwood

unread,
May 13, 2009, 11:09:13 AM5/13/09
to iphonesdkd...@googlegroups.com
Hi guys, I'm new here and I have a question.

I'm trying to use an operation object to create a thread. From the
looks of the NSInvocationOperation documentation I should use
initWithTarget:selector:object, which object is the argument to be
passed. Is it possible to pass multiple args? I can't figure it out:(

Thanks in advance for any suggestions!

deegee

unread,
May 13, 2009, 1:09:32 PM5/13/09
to iPhone SDK Development
No, you can pass only one object. Consider passing a NSArray of our
objects if you want to pass multiple NSStrings for example.

Brad Larson

unread,
May 13, 2009, 1:35:21 PM5/13/09
to iphonesdkd...@googlegroups.com


For a multi-argument method, you'll want to construct your
NSInvocation first, then use initWithInvocation: to create your
NSInvocationOperation from that.

The following is an example of how to construct an NSInvocation:

SEL theSelector = @selector(yourMethodWithString:number:)
NSMethodSignature * sig = [self methodSignatureForSelector:theSelector];
NSInvocation * theInvocation = [NSInvocation
invocationWithMethodSignature:sig];
[theInvocation setTarget:self];
[theInvocation setSelector:theSelector];

NSString *firstArgument = [NSString stringWithString:@"Test"];
NSNumber *secondArgument = [NSNumber numberWithInt:2];
[theInvocation setArgument:&firstArgument atIndex:2];
[theInvocation setArgument:&secondArgument atIndex:3];
[theInvocation retainArguments];

For more on NSInvocations, I direct you to Scott Stevenson's excellent
post on the topic:

http://theocacao.com/document.page/264
______________________
Brad Larson, Ph.D.
Sunset Lake Software
http://www.sunsetlakesoftware.com

Reply all
Reply to author
Forward
0 new messages