musictheory / NilScript

Objective-C-style language superset of JavaScript with a tiny, simple runtime
Other
50 stars 5 forks source link

Creating a class froms tring #56

Closed IngwiePhoenix closed 9 years ago

IngwiePhoenix commented 9 years ago

I am working on restructuring my code a little and wanted to implement something similar to NSApplicationMain. But I realized I would need to create a class from a string. How do I do that in OJ?

I.e.:

function StartApp(className) {
    var theClass = [Create a new class isntance using the string in ClassName];
}

Kind regards, Ingwie.

iccir commented 9 years ago

Unlike Obj-C, oj doesn't have a concept of looking up strings from classes/selectors and vice versa at runtime. oj.class_getName and oj.sel_getName exist, but they are for logging/debugging purposes only and will return a short name when --squeeze is enabled.

Hence, it's best to pass in a Class rather than a String. For example, here's what our ApplicationMain() looks like:

function ApplicationMain(cls) {
    var app = [UIApplication sharedInstance];
    var delegate = [[cls alloc] init];

    [app setDelegate:delegate];
    [app _launch];

    global.sApp = app;
}

ApplicationMain([AppDelegate class]);
IngwiePhoenix commented 9 years ago

Copy that. I copied the approach into my own app. Thanks! :)