Search code examples
objective-cpropertiesblockcategories

How to implement block property of Objective C category


Given:

@interface NSArray (Sample)

@property (nonnull, nonatomic, readonly) NSArray *_Nonnull (^mapped)(id __nullable (^block)(id __nonnull));

@end

How can one implement this category? I'm confused by this block property syntax. This explains the type annotations: https://developer.apple.com/swift/blog/?id=25

This is what I started for implementation:

@implementation NSArray (Sample)

typedef id __nullable (^block)(id __nonnull);

...
@end

later tried this:

@implementation NSArray (Sample)

typedef NSArray *_Nonnull (^mapped)( id __nullable (^block)(id __nonnull) );

-(mapped)mapped {
    return ^( id __nullable (^block)(id __nonnull) ){
        return @[@"what", @"the", @"heck"];
    };
}
@end

Later still:

Technically I think the above would fulfill the extension's contract, but per the comments with bbum I have tried to ascertain what the intent might likely be to create this kind of extension. Picked apart:

  1. The property is for a closure that takes a closure argument, and returns an NSArray.
  2. In the implementation we are creating the getter for this closure as per the readonly property attribute.

Normally we would inject/set the block with the setter however to fulfill the contract we could just construct it as an instance variable "someMapped" as follows.

@implementation NSArray (Sample)

typedef NSArray *_Nonnull (^mapped)( id __nullable (^block)(id __nonnull) );

-(mapped)mapped {

   //Normally someMapped block definition would be injected/set by the setter -(void) setMapped:(mapped) aMapped {
   mapped someMapped = ^(id __nonnull someId) {
       NSMutableArray * new = [[NSMutableArray alloc] init];
       for( NSMutableDictionary* dict in self) {
           NSMutableString * str = [dict objectForKey:someId];
           [str stringByAppendingString:@".png"];
           [new addObject:str];
       }
     return [new copy];
    };

  return someMapped;
}

@end

Solution

  • It isn't clear what you are trying to do. Are you trying to implement a map type function on NSArray? If so, then there is no need for an @property, which would imply that you are storing something associated with they instance. You probably just want a method:

    typedef id __nullable (^bt_MapBlockT)(id __nonnull);
    - (NSArray *)bt_map:(bt_MapBlockT) block;
    // written in browser... probably hosed the syntax slightly.
    

    There are a slew of examples floating about that implement map-reduce-filter-whatever on NSArray, btw.