objective-cfor-loopnsdictionaryfast-enumeration

How to do fast enumeration to populate an NSDictionary using two NSArray's in Objective-C?


I have two arrays, one that holds key values (myKeys), and the other holds NSString objects(myStrings). I would like to use both arrays to populate a single NSDictionary (myDictionary)using fast enumeration but am not sure how?

for (NSNumber *key in myKeys) {

   [self.myDictionary setObject @"here is where the value should go from the 'other' array forKey: key];

}

How would I factor in the NSArray object here?


Solution

  • Check the documentation, NSDictionary can do this without enumeration.

    NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:myObjects forKeys:myKeys];
    

    If you're trying to add values to an existing mutableDictionary, it can do that too.

    [mutableDictionary addEntriesFromDictionary:dictionary];