Re: How does Cocoa implement delegate/callback?
Re: How does Cocoa implement delegate/callback?
- Subject: Re: How does Cocoa implement delegate/callback?
- From: Chris Hanson <email@hidden>
- Date: Fri, 15 Nov 2002 00:08:32 -0600
It's fairly simple to use an NSInvocation. Just get a method
signature from the target of the invocation, create an invocation
using that signature, set the selector, set any arguments, invoke the
invocation with the target, and get the return value from it.
The code below is adapted from -[BDKeyValueQualifier
evaluateWithObject:] in my BDControl framework:
- (BOOL)sendSelector:(SEL)selector
toTarget:(id)target
withValue:(id)value
{
BOOL result = NO;
if ([target respondsToSelector:selector]) {
NSMethodSignature *signature;
NSInvocation *invocation;
signature = [target methodSignatureForSelector:selector];
invocation = [NSInvocation
invocationWithMethodSignature:signature];
[invocation setSelector:selector];
// (Arguments 0 and 1 are self and _cmd, respectively.)
[invocation setArgument:&value atIndex:2];
[invocation invokeWithTarget:target];
[invocation getReturnValue:&result];
}
return result;
}
It assumes that selector is a selector corresponding to a method
taking one id argument and returning a BOOL.
-- Chris
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.