Get array of property value of each object in another array without a for loop Get array of property value of each object in another array without a for loop ios ios

Get array of property value of each object in another array without a for loop


Yes. Assuming that your object is adopting the KVC/KVO protocol. You can get an array of the properties like:

NSArray *colorList = [carArray valueForKey:@"color"];

Actually, what valueForKey: method does, is to return an array containing the results of invoking valueForKey: using key on each of the array's objects. (From Apple's Documentation on NSArray)


Yes. You can do that without iterating it.

NSArray *colorArray = [carArray valueForKeyPath:@"@distinctUnionOfObjects.color"];


You can use the NSSet to get the colours:

NSSet *NScolors = [NSSet setWithArray:[carArray valueForKey:@"color"]];NSArray *colors = [NScolors allObjects];