Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A block winds up being an instance of type <code>__NSGlobalBlock__</code>, as seen in the following snippet:</p> <pre> void (^aBlock)(void) = ^(void) { NSLog(@"Hello world"); }; // prints "type = __NSGlobalBlock__" NSLog(@"type = %@", [aBlock class]); </pre> <p>In order to create a category of a class, the compiler needs to be able to see the original <code>@interface</code> declaration of the class. I can't find the declaration for <code>__NSGlobalBlock__</code> and probably for good reason.</p> <p><a href="http://cocoawithlove.com/2009/10/how-blocks-are-implemented-and.html" rel="nofollow">This article</a> and <a href="http://www.mikeash.com/pyblog/friday-qa-2009-08-14-practical-blocks.html" rel="nofollow">this article</a> contain some useful information about the implementation of blocks.</p> <p>To your original point, why not just make a category of <code>NSArray</code> for your <code>mapTo</code> method? It seems like a better place for that sort of functionality.</p> <p><em>Updated</em></p> <p>Let's say you <em>can</em> add a category to the Block object. How would you invoke the block from the category's method? To the best of my understanding, the only way to invoke a block is via the <code>()</code> operator (e.g., <code>aBlock()</code>). I don't think there's a way to tell from the Block object the number and types of parameters. So, what arguments would you pass in to the block invocation?</p> <p>I'm not recommending you do this, but the following works...</p> <pre> @interface NSObject (BlockExtension) - (void)foo; @end @implementation NSObject (BlockExtension) - (void)foo { // not sure how else to determine if self is a Block since neither // __NSGlobalBlock__ nor any of its superclasses (except NSObject) // are accessible to the compiler if ([[[self class] description] isEqual:@"__NSGlobalBlock__"]) { NSLog(@"foo"); // now what? // can't call self(), it doesn't compile // how else can I invoke this block? } } @end ... void (^aBlock)(void) = ^(void) { NSLog(@"Hello world"); }; // prints "foo" [aBlock foo]; </pre>
 

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