Don't like ads? PRO users don't see any ads ;-)

Swizzle Method

By: priore on Aug 1st, 2012  |  syntax: Objective C  |  size: 0.51 KB  |  hits: 54  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // www.prioregroup.com
  2. #import <objc/runtime.h>
  3.  
  4. void Swizzle(Class c, SEL orig, SEL newClassName)
  5. {
  6.     Method origMethod = class_getInstanceMethod(c, orig);
  7.     Method newMethod = class_getInstanceMethod(c, newClassName);
  8.     if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
  9.                 class_replaceMethod(c, newClassName, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
  10.         else
  11.                 method_exchangeImplementations(origMethod, newMethod);
  12. }