I have an array of dictionaries. I would like to extract an array with all the elements of 1 key of the dictionaries in the original array? Could this be done without enumeration?
+7
A:
Yes, use the NSArray -valueForKey:
method.
NSArray *extracted = [sourceArray valueForKey:@"a key"];
David Gelhar
2010-04-21 05:05:40
+1 You learn something every day!
e.James
2010-04-21 05:07:13
Thank you very much!
Run Loop
2010-04-21 05:07:32
+2
A:
Yes, just use Key-Value Coding to ask for the values of the key:
NSArray* names = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:
@"Joe",@"firstname",
@"Bloggs",@"surname",
nil],
[NSDictionary dictionaryWithObjectsAndKeys:
@"Simon",@"firstname",
@"Templar",@"surname",
nil],
[NSDictionary dictionaryWithObjectsAndKeys:
@"Amelia",@"firstname",
@"Pond",@"surname",
nil],
nil];
//use KVC to get the names
NSArray* firstNames = [names valueForKey:@"firstname"];
NSLog(@"first names: %@",firstNames);
Rob Keniger
2010-04-21 05:06:41