0

I'm looking to access the keys/elements from a dictionary in random, non-repeating, order. What's the shortest bit of ios code to achieve this?

blueberryfields
  • 45,910
  • 28
  • 89
  • 168

1 Answers1

3

Once you have NSMutableArray's -shuffle category from canonical way to randomize an NSArray in Objective C.

NSMutableArray *suffledKeys = [[dict allKeys] mutableCopy];
[suffledKeys shuffle];
for (id randomKey in shuffledKeys) {
    id randomValue = [dict objectForKey:randomKey];
    // What ever you need to do.
}

Hope this helps.

Community
  • 1
  • 1
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117