NSDictionary
can do this with an assist from NSArray
and KVC.
Try this:
NSDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjects:myArray
forKeys:[myArray valueForKey:@"time"];
That should work, since NSArray's version of -valueForKey:
returns an array of the values returned by each of it's elements for the key.
Caution: While the code above is concise and seems to solve your problem, it's not without some pitfalls. The main one is that a dictionary can only have one value for a given key, which means that if two or more of the entries in the array have the same value for the key @"time"
, only one of them (probably the last) will be represented in the resulting dictionary. Also, if any entry is missing a value for @"time"
, that entry will have [NSNull null]
as its key. As above, if there are two or more missing the time
value, only one will be represented in the dictionary.
Edit: Looking more closely at your question, I see that the times in the items are not necessarily distinct, so Hot Licks' answer will probably make more sense for you.