NSDictionary 可以做到这一点吗?或者我应该使用其他东西?

发布于 2024-09-15 13:23:19 字数 340 浏览 4 评论 0原文

我想要做的是创建一个文本字符串数组,并且我想从该数组中选择一个随机文本字符串,这通常可以使用数组轻松完成。但是,我也希望能够将它们放入类别中,当我随机选择一个类别时,我想知道它属于哪个类别,并对其做一些不同的事情,这由它所在的类别决定。我想我可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

姐不稀罕 2024-09-22 13:23:19

我只是用字典填充数组,即

{
  { category = Animals,
    name = Cat },
  { category = Vehicles,
    name = Helicopter },
  { category = Foods
    name = Pie },
  { category = Animals,
    name = Zebra }
}

然后从该数组中随机选择。

对于一个编程示例:

theArray = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"Animals", @"category", @"Cat", @"name", nil],
                                     [NSDictionary dictionaryWithObjectsAndKeys:@"Vehicles", @"category", @"Helicopter", @"name", nil],
                                     // ...
                                     [NSDictionary dictionaryWithObjectsAndKeys:@"Animals", @"category", @"Zebra", @"name", nil],
                                     nil];

// ...

randomDict = [theArray objectAtIndex:(rand() % [theArray count])];
NSString *name = [randomDict objectForKey:@"name"];
NSString *category = [randomDict objectForKey:@"category"];

I would just fill the array with dictionaries, i.e.

{
  { category = Animals,
    name = Cat },
  { category = Vehicles,
    name = Helicopter },
  { category = Foods
    name = Pie },
  { category = Animals,
    name = Zebra }
}

and then select randomly from that array.

For a programmatic example:

theArray = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"Animals", @"category", @"Cat", @"name", nil],
                                     [NSDictionary dictionaryWithObjectsAndKeys:@"Vehicles", @"category", @"Helicopter", @"name", nil],
                                     // ...
                                     [NSDictionary dictionaryWithObjectsAndKeys:@"Animals", @"category", @"Zebra", @"name", nil],
                                     nil];

// ...

randomDict = [theArray objectAtIndex:(rand() % [theArray count])];
NSString *name = [randomDict objectForKey:@"name"];
NSString *category = [randomDict objectForKey:@"category"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文