I want to create a plug-in system using objective-c. I am getting list of all classes in the main bundle and checking for classes derived from plug-in base.
int count = objc_getClassList(NULL, 0);
Class * buffer = (Class *)malloc(sizeof(Class) * count);
objc_getClassList(buf, count);
NSMutableArray * classNameArray = [NSMutableArray array];
for (int i = 0; i < count; i++)
{
[buf[i] isDerivedFromClass:[PluginBase class]]; //<<< I need this
NSString * classname=NSStringFromClass(buf[i]);
[classNameArray addObject:classname];
}
Is there any equivalent method for that?
While you can use
isKindOfClass:to do exactly the kind of checking you describe, are you sure this is what you need? A bundle can declare any class as its principle class, and if that class supplies the same methods as your plugin base class then it can be used as such (this is Objective-C’s duck typing). Therefore you could be testing, rather than for subclasses, for existence of the plugin’s API methods.It also looks like you’ve missed out on using the principle class. Looking at the documentation, a bundle can declare in its
Info.plistfile a class that represents the entry point to that bundle’s functionality. Rather than searching through every class looking for something compatible with your API, you can just require that plug-in authors register a compatible principle class. This approach allows plug-in authors to declare multiple conforming classes for using patterns like Strategy, State or Decorator while still exposing a single entry point. Your approach described in your question would try to use all of those instances concurrently.