Last active
December 12, 2015 07:38
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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