How can I convert an NSArray
to an NSDictionary
, using an int field of the array\'s objects as key for the NSDictionary
?
I have created a simple library, called Linq to ObjectiveC, which is a collection of methods that makes this kind of problem much easier to solve. In your case you need the Linq-to-ObjectiveC toDictionary method, where your 'int' field is specified via a key selector:
NSDictionary* dictionary = [sourceArray toDictionaryWithKeySelector:^id(id item) {
return [item intField];
}];
This returns a dictionary where the keys are given by the intField
in the source array.