I have an NSArray containing n elements at indices 0, 1 ... n-1. I want to populate an NSDictionary with the contents of my array.
Specifically the dictionary should contain key-value pairs where the key is the hash of the ith element in the array and the value is the index into the array.
For example: array = [123, 101, 199] then the dictionary will contain three key-value pairs:
([123 hash], 0)([199 hash], 2)([101 hash], 1)
I’ve done this with a for loop over the array. What’s a more concise way to do this? Perhaps something from NSKeyValueCoding?
More info: I’m thinking of something like this:
NSArray *keys = [myArray valueForKey:@"hash"];
NSArray *values = [myArray valueForKey:@"index"]; // @"index" needs to change
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:values
forKeys:keys];
I would probably use something like David’s
forloop solution myself, but just for kicks, and because I’m still trying to wrap my head completely around them, I came up with a solution usingblocks:I’m assuming the existence of a function
hashForthat generates the hash. You can replace that part with a message toobjor whatever you do to generate the hash.