How to implement block property of Objective C category

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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:



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





This is a very helpful reference when it comes to block syntax. goshdarnblocksyntax.com
– particleman
Aug 10 at 15:22





@particleman yes I have seen this but I still am not getting one in my OP.
– bhartsb
Aug 10 at 16:55




1 Answer
1



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:


map


NSArray


@property


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.





It is exactly as stated a property as per the @interface declaration. I had a question like this on a coding challenge so I'm trying to figure it out what it is trying to do too. But changing the intention is not the answer.
– bhartsb
Aug 11 at 20:09






@bhartsb Then the question on the challenge is wrong. That happens. The correct answer is to change the intention because the intention is wrong. There is absolutely no reason to store the block and, thus, using an @property is incorrect.
– bbum
Aug 12 at 0:59


@property





@bhartsb Feel free to refer the author of the coding challenge to me. I'm happy to help them correct the challenge.
– bbum
Aug 12 at 1:01






The block name map should be mapped, but I don't think that should make any difference. I'll PM you on LinkedIn.
– bhartsb
Aug 12 at 7:56





The question said "implement the following category" and "pay special attention to the interface: this is the contract you need to meet". I modified my OP to show the alternate way that I think meets the contract, but doing this is unfamiliar to me. As in I'm not getting the point of it.
– bhartsb
Aug 12 at 8:33







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard