NSDictionary 可以做到这一点吗?或者我应该使用其他东西?
我想要做的是创建一个文本字符串数组,并且我想从该数组中选择一个随机文本字符串,这通常可以使用数组轻松完成。但是,我也希望能够将它们放入类别中,当我随机选择一个类别时,我想知道它属于哪个类别,并对其做一些不同的事情,这由它所在的类别决定。我想我可以使用 NSDictionary 中的键来决定类别,就像将类别中的所有条目设置为具有相同的键一样。但我不知道如何从该字典中检索随机字典,然后知道密钥是什么。我从未使用过 NSDictionary,所以我对它了解不多,所以也许我刚才说的没有任何意义。
我也有可能以完全错误的方式处理这个问题,所以如果您对如何执行我所描述的操作有任何其他建议,我会对此持开放态度,如果可能的话,最好提供一个非常详细的代码答案。
What I am trying to do is create a an array of text strings, and I want to select a random text string out of this array, which I could normally do easily with an array. However, I would also like to be able to put them into categories, and when I select a random one, I would like to know what category it is in and do something different with it which is decided by the category it was in. I was thinking I could use keys in NSDictionary to decide the categories as in setting all the entries in a category to have the same key. But then I don't know how I could retrieve a random one from that dictionary and then know what the key was. I have never used NSDictionary so I don't know much about it so maybe what I just said doesn't make any sense.
It is also possible that I am approaching this in completely the wrong way, so if you have any other suggestions as to how to do what I described I would be open to that, and a pretty detailed code answer would be best if it is possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只是用字典填充数组,即
然后从该数组中随机选择。
对于一个编程示例:
I would just fill the array with dictionaries, i.e.
and then select randomly from that array.
For a programmatic example: