Access to objects in NSMutableDictionary by index

In order to display the key / values ​​from NSMutableDictionary sequentially (as a table), I need to access them by index. If access by index can give a key in that index, I could get a value. Is there a way to do this or another method?

+52
objective-c iphone cocoa-touch xcode
Sep 25 '09 at 6:26
source share
2 answers

You can get an NSArray containing the keys of an object using the allKeys method. Then you can examine it by index. Note that the order in which keys are displayed in the array is unknown. Example:

NSMutableDictionary *dict; /* Create the dictionary. */ NSArray *keys = [dict allKeys]; id aKey = [keys objectAtIndex:0]; id anObject = [dict objectForKey:aKey]; 

EDIT: Actually, if I understand what you're trying to do what you want, it's easy to do with a quick enumeration, like this:

 NSMutableDictionary *dict; /* Put stuff in dictionary. */ for (id key in dict) { id anObject = [dict objectForKey:key]; /* Do something with anObject. */ } 

EDIT: Fixed typo marked by Marco.

+118
Sep 25 '09 at 6:30
source share

You can get an array of all keys using the allKeys method of the dictionary; and then you can access the array by index. however, the dictionary itself does not have a built-in order, so the ordering of the keys that you receive before and after changing the dictionary can be completely different.

+9
Sep 25 '09 at 6:29
source share



All Articles