priore

How to invoke a method in a class via their string names

Jun 26th, 2013
639
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. + (void)invokeMethodNamed:(NSString*)methodName withObject:(id)object classNamed:(NSString*)className
  2. {
  3.                     id class = NSClassFromString(className);
  4.                     if (class) {
  5.                         SEL selector = NSSelectorFromString(methodName);
  6.                         if ([class respondsToSelector:selector]) {
  7.                             // static method +(void)
  8.                             [class performSelector:selector withObject:object];
  9.                         } else {
  10.                             // new instanced method -(void)
  11.                             id object = [[[class alloc] init] autorelease];
  12.                             if ([object respondsToSelector:selector]) {
  13.                                 [object performSelector:selector withObject:object];
  14.                             }
  15.                         }
  16.                     }
  17. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×