blueberryfields
blueberryfields

Reputation: 50468

What's the shortest bit of code that returns keys/elements from a dictionary in random order?

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?

Upvotes: 0

Views: 54

Answers (1)

Jeffery Thomas
Jeffery Thomas

Reputation: 42598

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.

Upvotes: 3

Related Questions