Add a map/apply method for NSArray

Originator:robnapier
Number:rdar://14807617 Date Originated:22-Aug-2013
Status:Duplicate: 6323801 (Open) Resolved:
Product: Product Version:
Classification: Reproducible:
 
Mapping an operation onto an array to create a new array is a very common problem that is unnecessarily cumbersome in ObjC. NSArray should have a built-in map-style methods such as:


Selector based (based on makeObjectsPerformSelector:): 

    - (NSArray *)arrayByApplyingSelector:(SEL)selector;
    - (NSArray *)arrayByApplyingSelector:(SEL)selector withObject:(id)argument;

    NSArray *names = [persons arrayByApplyingSelector:@selector(name)];


Block based (based on enumerateObjectsUsingBlock:):

    - (NSArray *)arrayByApplyingBlock:(id(^)(id obj, NSUInteger idx, BOOL *stop))block;

    NSArray *result = [numbers arrayByApplyingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
      return @([obj floatValue] + 5);
    }];


Function based (based on sortedArrayUsingFunction:...)

    - (NSArray *)arrayByApplyingFunction:(id (*)(id, void *))function context:(void *)context;

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!