[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to call objective-c method from c ?
From: |
Richard Frith-Macdonald |
Subject: |
Re: How to call objective-c method from c ? |
Date: |
Thu, 16 Oct 2003 19:18:15 +0100 |
On Thursday, October 16, 2003, at 06:42 PM, Yen-Ju Chen wrote:
Hi,
How to call objective-c method from c function ?
To work in a manner independent of the objective-c runtime in use,
you can do stuff like this -
#include <GNUstepBase/GSObjCRuntime.h>
// Get the objc class
Class class = GSClassFromName("TestObject");
// Get the +alloc selector
SEL selector = GSSelectorFromname("alloc");
// Get the method definition for the alloc selector from the class
GSMethod method = GSGetClassMethod(class, selector);
// Call the +alloc method implementation to return an instance of the
class
id instance = (*method->method_imp)(class, selector);
// now initialise the object
selector = GSSelectorFromname("init");
method = GSGetInstanceMethod(class, selector);
instance = (*method->method_imp)(instance, selector);
Otherwise, you need to do similar things with runtime specific function
calls ...
basically, get a class, get selectors by name (I don't think you can
use compiler
support like @selector() from plain C), ask the class for the actual
method
implementation (a function pointer) and call that implementation passing
the reveiver and the selector as the first two arguments.
CAVEAT ... I haven't actually done this using the macros in
GSObjCRuntime.h
but that's how they are supposed to work and a lot of them are used
internally
by the base library, so they should be fine. David Ayer has done lots
of good
new stuff there, and I expect he'd help and fix things if I can't.