从 .plist 中的随机字典中获取 2 个字符串

发布于 2024-10-23 22:17:28 字数 1068 浏览 6 评论 0原文

我是 iPhone 编程新手。我一直在努力解决这个问题,并尝试了很多不同的在线解决方案,但无法得到想要的结果。

我想显示随机数组或字典中的 2 个字符串(我不确定最好使用什么)它将显示一个随机问题和配对答案。这是我到目前为止所拥有的:

<dict>
<key>q2</key>
<array>
    <string>answer2</string>
    <string>question2</string>
</array>
<key>q1</key>
<array>
    <string>answer1</string>
    <string>question1</string>
</array>

.m:

        NSString *fileContents = [[NSBundle mainBundle] pathForResource:@"questions" ofType:@"plist"];

    NSMutableDictionary *plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:fileContents];

    NSMutableArray *array = [plistDict objectForKey:@"q1"];

    srandom(time(NULL));

    int r = arc4random() %[array count];

    NSString *arrayData1 = [array objectAtIndex:r];
    NSString *arrayData2 = [array objectAtIndex:r+1];

    label1.text = arrayData1;
    label2.text = arrayData2;

这显示了正确的结果。但显然它只是从“q1”数组中挑选出来。我希望能够从任何数组中获取它。任何帮助将不胜感激。谢谢。

I am new to iphone programming. I have been struggling with this problem and have tried so many different online solutions but can't get the desired result.

I want to display 2 strings from a random array or dictionary (i'm not sure what is best to use) It would show a random question with the paired answer. Here's what i have so far:

<dict>
<key>q2</key>
<array>
    <string>answer2</string>
    <string>question2</string>
</array>
<key>q1</key>
<array>
    <string>answer1</string>
    <string>question1</string>
</array>

.m:

        NSString *fileContents = [[NSBundle mainBundle] pathForResource:@"questions" ofType:@"plist"];

    NSMutableDictionary *plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:fileContents];

    NSMutableArray *array = [plistDict objectForKey:@"q1"];

    srandom(time(NULL));

    int r = arc4random() %[array count];

    NSString *arrayData1 = [array objectAtIndex:r];
    NSString *arrayData2 = [array objectAtIndex:r+1];

    label1.text = arrayData1;
    label2.text = arrayData2;

This shows the correct result. But obviously its only picking it out of the 'q1' array. I would like to be able to get it from any array. Any help would be greatly appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

甜味超标? 2024-10-30 22:17:28

这段代码如何“显示正确的结果”?由于此代码引起的越界异常,它应该每秒(使用您提供的测试数据)调用崩溃:

                                                    // assume array has 10 objects
int r = arc4random() %[array count];                // r = 9 

NSString *arrayData1 = [array objectAtIndex:r];     // index 9, everything ok
NSString *arrayData2 = [array objectAtIndex:r+1];   // index 9 + 1 = 10. exception


如果我是你,我会从根本上更改代码和数据结构。对于您的问题使用 NSArray 并针对每个单独的问题使用 NSDictionary 更有意义。
如果字典中的键名为 q1、q2、q3、q4 等,则没有理由使用 NSDictionary。

然后你可以使用这样的东西,它更容易理解并且更干净。

NSString *pathToQuestions = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];
NSMutableArray *questions = [[[NSMutableArray alloc] initWithContentsOfFile:pathToQuestions] autorelease];

int questionIndex = arc4random() %[questions count];

NSDictionary *question = [questions objectAtIndex:questionIndex];
NSString *answerStr = [question objectForKey:@"answer"];
NSString *questionStr = [question objectForKey:@"question"];

How could this code "show the correct result"? It should crash every second (with the test data you provided) call because of an out of bounds exception caused by this code:

                                                    // assume array has 10 objects
int r = arc4random() %[array count];                // r = 9 

NSString *arrayData1 = [array objectAtIndex:r];     // index 9, everything ok
NSString *arrayData2 = [array objectAtIndex:r+1];   // index 9 + 1 = 10. exception


If I were you I would radically change the code and the structure of the data. It makes much more sense to use a NSArray for your question and a NSDictionary for each individual question.
If the keys in a dictionary are named q1, q2, q3, q4, and so on there is no reason to use a NSDictionary.

Then you could use something like this, which is much easier to understand and much cleaner.

NSString *pathToQuestions = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];
NSMutableArray *questions = [[[NSMutableArray alloc] initWithContentsOfFile:pathToQuestions] autorelease];

int questionIndex = arc4random() %[questions count];

NSDictionary *question = [questions objectAtIndex:questionIndex];
NSString *answerStr = [question objectForKey:@"answer"];
NSString *questionStr = [question objectForKey:@"question"];
做个ˇ局外人 2024-10-30 22:17:28

由于您使用的是 NSMutableArray *array = [plistDict objectForKey:@"q1"]; 仅采用 q1 中的数组。您必须通过随机选择字典的键来随机获取数组。

更新

例如,如果您有 7 个名为 q1、q2、q3、q4、q5、q6、q7 的问题数组。你必须从中随机选择一个数组。所以你可以使用

int q = arc4random() %[[plistDict allKeys] count];
NSMutableArray *randomQuestionarray = [[plistDict objectForKey:[NSString stringWithFormat:@"q%d",q]];

这将为你提供问题的随机数组

Since you are using NSMutableArray *array = [plistDict objectForKey:@"q1"]; only the array from q1 will be taken. You have to obtain the array randomly by choosing the key for the dictionary randomly.

UPDATE

For example if u have say 7 arrays of questions named q1,q2,q3,q4,q5,q6,q7. you have to choose an array randomly from this. So you can use

int q = arc4random() %[[plistDict allKeys] count];
NSMutableArray *randomQuestionarray = [[plistDict objectForKey:[NSString stringWithFormat:@"q%d",q]];

This will give you the random array for the questions

梦在深巷 2024-10-30 22:17:28

@7KV7 是正确的,您在运行时将需要随机格式化的密钥。

对于随机格式的密钥,您需要密钥的起始文本,在您的情况下它是 q 和密钥数量,

例如,假设您总共有 50 个密钥,您的密钥值必须是这样的
q0,q1,q2,----- q49。

#include <stdlib.h>
...
...


NSInteger myRandomInt  = arc4random() % numberOfKeys ;
NSString myRandomKey = [[NSSting alloc]  initWithFormat:@"q%d", myRandomInt];

NSMutableArray *array = [plistDict objectForKey:myRandomKey];

@7KV7 is right,you will need randomly formated key's at run time.

For andomly formated key's, you require the starting text of your key, in your case it's q and number of keys,

for example let's suppose you have total 50 key the your key values must be like this
q0,q1,q2, ----- q49.

#include <stdlib.h>
...
...


NSInteger myRandomInt  = arc4random() % numberOfKeys ;
NSString myRandomKey = [[NSSting alloc]  initWithFormat:@"q%d", myRandomInt];

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