抢NSDictionary中的随机条目 [英] Grab random entry in NSDictionary
本文介绍了抢NSDictionary中的随机条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在NSDictionary中获取完全随机的密钥?
Is there a way to grab a totally random key in the NSDictionary?
NSString *key = [enumeratorForKeysInDictionary nextObject];
我有这段代码以非随机的方式遍历字典.
I have this code which iterates over the dictionary in a non-random way.
我应该将所有密钥添加到NSSet中,然后从那里随机拉出吗?
Should I add all the keys to an NSSet and then pull randomly from there?
有没有更有效的方法?
推荐答案
查看此内容:
NSArray *array = [dictionary allKeys];
int random = arc4random()%[array count];
NSString *key = [array objectAtIndex:random];
这篇关于抢NSDictionary中的随机条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文