Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>@pwc is correct in that you can't create a category for a class that you can't see.</p> <p>However...</p> <h1>WHAT I AM ABOUT TO TELL YOU SHOULD BE USED STRICTLY AS AN EXERCISE IN LEARNING, AND <em>NEVER</em> IN ANY SORT OF PRODUCTION SETTING.</h1> <ol> <li>Some runtime introspection reveals some interesting information. There are a number of classes that contain the word "Block". Some of them look promising: <code>__NSStackBlock</code>, <code>__NSMallocBlock</code>, <code>__NSAutoBlock</code>, and <code>NSBlock</code>.</li> <li>Some more introspection shows that the promising classes inherit from <code>NSBlock</code></li> </ol> <p>So it looks like any block is going to be some instance or subclass of <code>NSBlock</code>.</p> <p>You can create a method on an object, like so:</p> <pre><code>@implementation Foo - (void) doFoo { //do something awesome with self, a block //however, you can't do "self()". //You'll have to cast it to a block-type variable and use that } @end </code></pre> <p>Then at runtime, you can move that method to the <code>NSBlock</code> class:</p> <pre><code>Method m = class_getInstanceMethod([Foo class], @selector(doFoo)); IMP doFoo = method_getImplementation(m); const char *type = method_getTypeEncoding(m); Class nsblock = NSClassFromString(@"NSBlock"); class_addMethod(nsblock, @selector(doFoo), doFoo, type); </code></pre> <p>After this, blocks should respond to the <code>doFoo</code> message.</p> <h1>USE AT YOUR OWN RISK, AND ONLY FOR EXPERIMENTING.</h1>
 

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