NSInvocation help
NSInvocation help
- Subject: NSInvocation help
- From: Gregory Weston <email@hidden>
- Date: Fri, 28 Dec 2001 08:31:06 -0500
Does anyone know of a short and sweet example of how to use NSInvocation? I
grabbed some sample code - can't remember where from - that almost works except
for one oddity. The method I've specified gets invoked on the object I've
specified, but the argument I'm passing (in index 2) is being delivered as if I
sent the object's class instead of the object itself.
Given t ( a member of a class that response to useFoo:)
and sel ( = @selector(useFoo:))
{
Foo* foo = [someFactoryObject gimmeOneFoo];
NSMethodSignature* s = [[t class] instanceMethodSignatureForSelector:sel];
NSInvocation* i = [NSInvocation invocationWithMethodSignature];
[i setSelector:s];
[i setTarget:t];
[i setArgument:foo atIndex:2];
[i invoke];
}
Now I add foo to the NSInvocation as argument #2, set up the target and method
and invoke. If I tell the PB debugger to print the description of foo, it gives
me what I expect (output from the Foo -description method). But in the invoked
method:
- (void)useFoo:(Foo*)inFoo
{
NSString* theName = [inFoo name];
//...
}
I end up with a message in the console telling me that the class Foo doesn't
respond to name (+[Foo name]: selector not recognized), and the instance
variables in inFoo aren't sane. Printing a description to the console gives me
"Foo." I've tried manually retaining foo in the method that sets up the
NSInvocation (and not). I've tried telling the NSInvocation to retain its
arguments (and not). I've even passed instances of stock classes that respond to
name with similar effect.
Any help?
G