Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is no way (anymore)to simulate inheritance without subclassing. There use to be Posing, method swizzling is all that is left (not as elegant as posing though). Here is one way to do method swizzling correctly while being able to call the original implementation. </p> <pre><code>int swizzle_instance_methods(Class class, SEL selector, IMP replacement, IMP *store) { @synchronized(class) { Method method = class_getInstanceMethod(class, selector); IMP original_imp = NULL; if (method != NULL) { const char *type = method_getTypeEncoding(method); IMP original_imp = class_replaceMethod(class, selector, replacement, type); if (original_imp == NULL) original_imp = method_getImplementation(method); if (original_imp != NULL &amp;&amp; store != NULL) { *store = original_imp; } } return (original_imp != NULL); } } + (void) load { static IMP originalMethodImpl = NULL; IMP customMethodImpl = imp_implementationWithBlock(^(id self_) { NSString *descr = ((NSString(*)(id,SEL))originalMethodImpl)(self_, @selector(description); return [NSString stringWithFormat:@"&lt;--- %@ ---&gt;",descr]; }); swizzle_instance_methods([self class], @selector(description), customMethodImpl, &amp;originalMethodImpl); } </code></pre> <p>I might add that this is nice for debugging and I think that it can be greate for building excellent frameworks. Alas, Apple seems to think differently and using method swizzling can result in your app being excluded from the App store. If you are not aiming for the app store then all the power to you.</p>
    singulars
    1. This table or related slice is empty.
    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. 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