C As Principal Class For Mac App

Posted by CodaFi on Stack Overflow See other posts from Stack Overflow or by CodaFi
Published on 2012-07-03T21:05:09Z Indexed on 2012/07/03 21:15 UTC
Read the original article Hit count: 186

Filed under:
|
|
|
|

So, I've got a c file raring to go and be the main class behind an all-C mac-app, however, a combination of limiting factors are preventing the application from being launched. As it currently stands, the project is just a main.m and a class called AppDelegate.c, so I entered "AppDelegate" as the name of the principal class in the info.plist, and to my complete surprise, the log printed:

Unable to find class: AppDelegate, exiting

This would work perfectly well in iOS, because the main function accepts the name of a delegate class, and handles it automatically, but NSApplicationMain() takes no such argument.

Now, I know this stems from the fact that there are no @interface/@implementation directives in C, and that's really what the OS seems to be looking for, so I wrote a simple NSApplication subclass and provided it as the Principal Class to the plist, and it launched perfectly well. My question is, how could one go about setting a c file as the principal class in a mac application and have it launch correctly?

PS, don't ask what or why I'm doing this for, the foundation must be dug.

For @millimoose's amusement, here be the AppDelegate.c file:

#include <objc/runtime.h>
#include <objc/message.h>

struct AppDel
{
    Class isa;

    id window;
};


// This is a strong reference to the class of the AppDelegate
// (same as [AppDelegate class])
Class AppDelClass;

BOOL AppDel_didFinishLaunching(struct AppDel *self, SEL _cmd, void *application, void *options) {


    self->window = objc_msgSend(objc_getClass("NSWindow"), sel_getUid("alloc"));
    self->window = objc_msgSend(self->window, sel_getUid("init"));


    objc_msgSend(self->window, sel_getUid("makeKeyAndOrderFront:"), self);


    return YES;
}

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about c