views:

476

answers:

1

When I was using an NSArray, it was easy:

NSArray *array = ...
lastIndex = INT_MAX;
...
int randomIndex;
do {
  randomIndex = RANDOM_INT(0, [array count] - 1);
} while (randomIndex == lastIndex);
NSLog(@"%@", [array objectAtIndex:randomIndex]);
lastIndex = randomIndex;

I need to keep track of the lastIndex because I want the feeling of randomness. That is, I don't want to get the same element twice in a row. So it shouldn't be "true" randomness.

From what I can tell, NSDictionary doesn't have something like -objectAtIndex:. So how do I accomplish this?

A: 

You can get an array of keys with allKeys (undefined order) or keysSortedByValueUsingSelector (if you want sorting by value). One thing to keep in mind (regarding lastIndex) is that even with sorting, the same index may come to refer to a different key-value pair as the dictionary grows.

Either of these (but especially keysSortedByValueUsingSelector) will come with a performance penalty.

EDIT: Since the dictionary isn't mutable, you should just be able to call allKeys once, and then just pick random keys from that.

Matthew Flaschen
It's an NSDictionary (not an NSMutableDictionary), so it's not going to grow. It will be explicitly created on app launch, using [[NSDictionary alloc] initWithObjectsAndKeys:..., nil];
Elliot
It works. What I did is call allKeys once and store it in a new NSArray ivar.It's strange that allKeys is not guaranteed to always return the keys in the same order, even if the NSDictionary is unchanged. Hopefully there's no problem with pointing an NSArray* at its return value, though.
Elliot
I don't see anything wrong with holding on to the allKeys return value in your case. A new array is allocated, so the result doesn't point directly at an internal data structure.
Matthew Flaschen