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:
- The property is for a closure that takes a closure argument, and returns an NSArray.
- 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