Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>UPDATE</strong>: (My App uses this method and is in the appstore)</p> <p>Method swizzling seems to be working as of May 30, 2012. this is my implementation.</p> <p>(This is for those of you looking around and finding bad code on wiki pages and just want a quick implementation.)</p> <p>Swizz.h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; void ActivateAutoSwizz(); void Swizz(Class c, SEL orig, SEL replace); @interface NSObject (swizz) // This Method allows the class to Swizzle more methods within itself. // And allows for an overridable init method in Class Extensions // ###################### // //// To Swizzle a method, call Swizzle once on the class in question. // //// dispatch_once is a good way to handle that. // static dispatch_once_t onceToken; // dispatch_once(&amp;onceToken, ^{ // Swizz([UITableViewCell class], @selector(reuseIdentifier), @selector(classReuseIdentifier)); // }); - (void) swizzInit; @end </code></pre> <p>Swizz.m</p> <pre><code>#import "Swizz.h" #import &lt;objc/runtime.h&gt; #import &lt;objc/message.h&gt; //.... void Swizz(Class c, SEL orig, SEL replace) { Method origMethod = class_getInstanceMethod(c, orig); Method newMethod = class_getInstanceMethod(c, replace); if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) class_replaceMethod(c, replace, method_getImplementation(origMethod), method_getTypeEncoding(origMethod)); else method_exchangeImplementations(origMethod, newMethod); } @implementation NSObject (swizz) // Load gets called on every object that has it. Off Thread, before application start. + (void) load { static dispatch_once_t onceToken; dispatch_once(&amp;onceToken, ^{ Swizz([NSObject class], @selector(init), @selector(initIntercept)); }); } - (id) initIntercept{ self = [self initIntercept]; // Calls the Original init method if(self){ [self swizzInit]; } return self; } - (void) swizzInit{ //Do Nothing.. Gives an extension point for other classes. } @end </code></pre> <p>I built this to allow me to intercept the reuseIdentifier on the UITableViewCell with a UITableViewCell extension.</p> <p>Here is that example.</p> <p>UITableViewCell+ReuseIdentifier.h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import &lt;Foundation/Foundation.h&gt; @interface UITableViewCell (ReuseIdentifier) @end </code></pre> <p>UITableViewCell+ReuseIdentifier.m</p> <pre><code>#import "UITableViewCell+ReuseIdentifier.h" #import "Swizz.h" @implementation UITableViewCell(ReuseIdentifier) // Edited to remove Class variable. // Class variables in Categories are Global. // And as it turns out, this method did not need the variable anyhow. - (NSString *)classReuseIdentifier{ NSString *reuseIdentifierResult = [self classReuseIdentifier]; // Gets the original reuseIdentifier if (reuseIdentifierResult == nil){ reuseIdentifierResult = [[self class] description]; } return reuseIdentifierResult; } // Alternatively you can use the +(void)load method on the class to achieve the same thing. - (void)swizzInit{ static dispatch_once_t onceToken; dispatch_once(&amp;onceToken, ^{ Swizz([UITableViewCell class], @selector(reuseIdentifier), @selector(classReuseIdentifier)); }); } @end </code></pre> <p>As you can see, the ActivateAutoSwizz() as well as my swizzInit method both use dispatch_once to execute the swizzle once.</p> <pre><code>static dispatch_once_t onceToken; dispatch_once(&amp;onceToken, ^{ Swizz([NSObject class], @selector(init), @selector(initIntercept)); }); </code></pre> <p>If you execute it twice. it reverses your method switch back to original. I hope this helps some of you iOS devs out there.</p> <p><strong>NOTE: I have determined that +(void) load is called once at app start and is a wonderful place to achieve the method swizzle. Unfortunately in some dev situations +(void)load is not called, You may want to test your app to make sure these methods are being called.</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload