I don't know if this is possible, but having code in plain C, is it possible to call Cocoa API's from it?
Something like #include <cocoa.h>
, add the corresponding library and go for it?
Thanks for the help
If you link against the Cocoa or Foundation frameworks, you can use objective-c within your C code. However, if you want to use the normal messaging syntax, you will have to change the file extension from .c to .m so that it is compiled as objective-c. If you keep the .c extension, you will only be able to use the c-style runtime calls to interact with objective-objects (i.e. objc_msgSend
, objc_getClass
, etc.).
Examples: Within a .m file
void cFunction() {
[[NSString alloc] init];
}
Within a .c file
void cFunction() {
void* cls = objc_getClass("NSString");
void* obj = objc_msgSend(cls, NSSelectorFromString(CFSTR("alloc")));
obj = objc_msgSend(obj, NSSelectorFromString(CFSTR("init")));
}
If you choose the second method, see the Objective-C Runtime Reference.
Objective-C is a very thin wrapper on top of C. The compiler just translates
[obj message:argument];
into a C-call
obj_msgSend(obj,@selector(message:),argument);
and that's it (where @selector(message:)
is a magic encoding turning a selector (method name) into a computer-understandable-thingy.)
As such, there is not much difference between Objective-C and C from the compiler's viewpoint. For example, you can compile a pure C program with an Objective-C compiler, with exactly the same result as you would get by compiling with a C compiler.
So, the easiest way to "mix" a bit of Objective-C with C is to use .m
extension so that the compiler use Objective-C.
That doesn't make your program suddenly very Objective-C-y. You can keep your program almost pure C, with the extension .m
. With .m
, you can add a few lines of Objective-C message calls without problems.