Sunday, 8 September 2013

Using Objective C classes from C code

Using Objective C classes from C code

I know it is possible to use Objective C code using objc_msgSend to, I
believe, manually run the Objective C runtime however when I run this code
I get errors referencing NSString (even though I never use it) as well as
other un-used classes.
I have the Objective C Code above it (commented out) that I am trying to
'mimic'.
#include <AppKit/AppKit.h>
int main()
{
// convert objective c into c code
/*
NSAlert *alert = [[NSAlert alloc] init];
[alert setAlertStyle:NSInformationalAlertStyle];
[alert setMessageText:@"Hello World"];
[alert setInformativeText:@"Hello World"];
[alert runModal];
*/
id alert = objc_msgSend(objc_msgSend(objc_getClass("NSAlert"),
sel_registerName("alloc")), sel_registerName("init"));
objc_msgSend(alert, sel_getUid("setAlertStyle:"),
NSInformationalAlertStyle);
objc_msgSend(alert, sel_getUid("setMessageText:"), CFSTR("Hello
World!"));
objc_msgSend(alert, sel_getUid("setInformativeText:"), CFSTR("Hello
World!"));
objc_msgSend(alert, sel_getUid("runModal"));
}

No comments:

Post a Comment