开发者

Get 2 strings from a random dictionary in a .plist

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</str开发者_JAVA百科ing>
</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.


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"];


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


@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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜