Reputation: 4496
NSClassFromString(aClassName)
returns a class object of the class named aClassName. Great.
Now how can I call a class method on that class object? For
Class moduleClass = NSClassFromString(aClassName);
Xcode won't allow me to either call
AppModule* appModuleClass = moduleClass;
[appModuleClass classMethod] // actually that's an object instance...
or
[((AppModule)moduleClass) classMethod]; // C-style cast not allowed
What am I missing here? Thanks.
Upvotes: 5
Views: 2960
Reputation: 11
You can use
__typeof([AppModule class]) cls = NSClassFromString(@"aClassName");
if ( [cls respondsToSelector:@selector(classMethod)] ) {
[cls classMethod];
}
Upvotes: 0