Re: Passing C Style Function Callbacks in Cocoa
Re: Passing C Style Function Callbacks in Cocoa
- Subject: Re: Passing C Style Function Callbacks in Cocoa
- From: Allan Odgaard <email@hidden>
- Date: Fri, 5 Mar 2004 17:31:08 +0100
On 5. Mar 2004, at 16:37, Vince Ackerman wrote:
[...] I'd rather be able to pass a call-back that was a method of my
object instead. Is there a way to pass a Obj C style message as a
function* in place of the C-style function???
Sort of... you can pass @selector(someMethod:) -- but that will only
give you the actual message to send, not to which object you need to
send it. E.g.:
void callback (id obj, SEL msg)
{
...
[obj performSelector:msg withObject:arg];
...
}
callback(obj, @selector(someMethod:));
Alternatively you can use methodForSelector:, this gives you a real
function pointer, but you still need to provide the object to which the
message should be sent (as first argument, and actually the selector as
second argument).
If you're into (Objective-)C++ you can use unary_method (from CocoaSTL)
and std::bind1st to bind the object to this functor, which semantically
gives you the same as a function pointer w/o requiring the object as
argument, e.g.:
template <typename F>
void callback (F function)
{
...
function(42); // actually calls [obj someMethod:42]
...
}
callback(bind1st(unary_method<int>("someMethod:"), obj));
_______________________________________________
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.