void Swizzle(Class c, SEL orig, SEL new){ Method origMethod = class_getInstanceMethod(c, orig); Method newMethod = class_getInstanceMethod(c, new); if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod)); else method_exchangeImplementations(origMethod, newMethod); }
via devinsheaven.com
How to Swizzle, the awesome way. Using a method like this allows similar functionality to Rails' alias_method_chain, allowing you to extend core system methods on-the-fly even though Apple doesn't tell you what those methods do. This is a powerful technique for extending functionality, and hooking in to tightly coupled classes like UINavigationController and UINavigationBar.