Re: ObjC from C
Re: ObjC from C
- Subject: Re: ObjC from C
- From: Nat! <email@hidden>
- Date: Fri, 14 Dec 2001 00:20:41 +0100
On Donnerstag, Dezember 13, 2001, at 07:42 Uhr, Rosyna wrote:
>
Is it possible for a C application (it starts as C) to start
>
calling ObjectiveC methods or move to ObjC?
>
>
like....
>
>
int main(int argc, const char *argv[])
>
{
>
return SomeLameCFunction("NO!");
>
}
>
>
bool SomeLameCFunction(char * bob)
>
{
>
// fall into cocoa here....
>
}
>
--
>
Assuming that you have a pressing need to keep some .c files
around, which must call Objective-C code.
You could do it the hard way like this...
#include <objc/objc.h>
#include <stdio.h>
extern Class objc_getClass( const char *name);
extern id objc_msgSend( id self, SEL op, ...);
extern SEL sel_getUid( const char *name);
int main (int argc, const char *argv[])
{
SEL selector;
Class aClass;
aClass = objc_getClass( argv[ 1]);
selector = sel_getUid( argv[ 2]);
printf( "[%s %s] returns %lX\n",
argv[ 1], argv[ 2], (unsigned long) objc_msgSend( (id)
aClass, selector));
return 0;
}
(gdb) Starting program: /Network/Users/nat/srcX/objcFromC/objcFromC
NSString retainCount
[Switching to thread 1 (process 582 thread 0x3403)]
Reading symbols for shared libraries ... done
[NSString retainCount] returns FFFFFFFF
Program exited normally.
No stack.
(gdb)
or just call a function that resides in a .m file from a .c file
main.c
...
callObjC();
foo.m
.....
#import "MyClass.h"
void callObjC()
{
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
[[MyClass alloc] init];
[pool release];
}
Cheers
Nat!
------------------------------------------------------
Some people drink deep from the fountains of life, and
some just gargle. -- DLR