CK knows Wayne!

Plugin-System in Obj-C

Published at by Christian Kruse, updated at
Filed under: Computer

Heute musste ich mir überlegen, wie ich unsere Software modularisiere. Also, konzeptionell, das war mir schon länger klar, sondern technisch. Eigentlich wollte ich einen statischen C-Array von Klassenobjekten machen, aber das scheint nicht möglich zu sein. Alternativ habe ich mir jetzt folgende Methode überlegt:

Ich speichere die Klassennamen als C-Strings in einem statischen Array. Bei applicationDidFinishLaunching: durchlaufe ich diesen Array und hole mir via objc_getClass() das Klassenobjekt. Das kann ich nun ganz normal instanzieren. Praktisch sieht das so aus:


static const char *pluginList[] = {
    "ModuleA",
    "ModuleB",
    "ModuleC",
    NULL
};

//…

- (void)applicationDidFinishLaunching:(UIApplication *)application {
  id plugin,*pluginInstance;
  int i;

  for(i=0;pluginList[i];++i) {
    plugin = objc_getClass(pluginList[i]);
    pluginInstance = [[pugin alloc] initWithDelegate:self];
    [pluginInstance release];
  }
}

Das Plugin kann sich dann beim Delegate mit entsprechenden Routinen registrieren. Ich muss sagen, Obj-C gefällt mir inzwischen wirklich.

No comments, yet

Comment Feed: RSS / Atom

Your Comment

You can use MarkDown to format your comment: *Word* for italic, **Word** for bold, images will get removed.




Because of massive spam attacks you may only post if you can answer the following question: