I would like to add functions by creating a category for Objective-C Blocks.
__block int (^aBlock)(int) = ^int( int n ){
if( n <= 1 ) return n;
return
@pwc is correct in that you can't create a category for a class that you can't see.
However...
__NSStackBlock
, __NSMallocBlock
, __NSAutoBlock
, and NSBlock
.NSBlock
So it looks like any block is going to be some instance or subclass of NSBlock
.
You can create a method on an object, like so:
@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
Then at runtime, you can move that method to the NSBlock
class:
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);
After this, blocks should respond to the doFoo
message.