Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling class method on a Class returned by NSClassFromString()

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.

like image 735
ATV Avatar asked Apr 26 '14 13:04

ATV


1 Answers

To call a class method do so:

[NSClassFromString(aClassName) performSelector:@selector(classMethod)];

Maybe this could work:

objc_msgSend(NSClassFromString(aClassName), @selector(classMethod));
like image 155
idmean Avatar answered Oct 31 '22 07:10

idmean