Skip to content

Instantly share code, notes, and snippets.

@swissmanu
Last active December 12, 2015 07:38
Show Gist options
  • Save swissmanu/4737931 to your computer and use it in GitHub Desktop.
Save swissmanu/4737931 to your computer and use it in GitHub Desktop.
Create a NSArray with a specific value of the items of another NSArray.
@interface NSArray (ExtractValuesAsArray)
/** Creates a new NSArray with the specific value of the items of another array. Ensure that the items of the array are KVC compliant since a key path is used to gather the values.
*/
+(NSArray*)arrayWithValuesOfArrayItems:(NSArray*)array keyPath:(NSString*)keyPath;
@end
#import "NSArray+ExtractValuesAsArray.h"
@implementation NSArray (ExtractValuesAsArray)
+(NSArray*)arrayWithValuesOfArrayItems:(NSArray *)array keyPath:(NSString *)keyPath {
NSMutableArray *values = [NSMutableArray arrayWithCapacity:array.count];
for (id item in array) {
id value = [item valueForKeyPath:keyPath];
if(value != nil) [values addObject:value];
}
return [NSArray arrayWithArray:values];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment