NSArray Equivalent of Map

后端 未结 11 1784
萌比男神i
萌比男神i 2020-11-28 05:06

Given an NSArray of NSDictionary objects (containing similar objects and keys) is it possible to write perform a map to an array of specified key?

11条回答
  •  借酒劲吻你
    2020-11-28 05:32

    For Objective-C, I would add the ObjectiveSugar library to this list of answers: https://github.com/supermarin/ObjectiveSugar

    Plus, its tagline is "ObjectiveC additions for humans. Ruby style." which should suit OP well ;-)

    My most common use-case is mapping an dictionary returned by a server call to an array of simpler objects e.g. getting an NSArray of NSString IDs from your NSDictionary posts:

    NSArray *postIds = [results map:^NSString*(NSDictionary* post) {
                           return [post objectForKey:@"post_id"];
                       }];
    

提交回复
热议问题