Is it possible to create a category of the “Block” object in Objective-C

前端 未结 5 1553
梦如初夏
梦如初夏 2021-02-07 20:40

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         


        
5条回答
  •  猫巷女王i
    2021-02-07 21:16

    Dave DeLong is right, you cannot add a category on a class that you cannot see, but as blocks are subclasses of NSBlock adding:

    @interface NSBlock : NSObject
    @end
    

    Now you can 'see' NSBlock and add a category on it, e.g.:

    @interface NSBlock (map)
    - (NSArray *)mapTo:(NSArray *)array;
    @end
    
    @implementation NSBlock (map)
    - (NSArray *)mapTo:(NSArray *)array
    {
        ...
    }
    @end
    

    Still probably not the best thing to do in code that is actually used in production...

提交回复
热议问题