简体   繁体   中英

How to extract a range from an Array of Dictionaries to randomise? (Objective-C)

I have an array of dictionaries stored in a plist with keys and strings, they are questions and answers for my Quiz app. I am putting a random question on screen but I want to put the answers of the question in a random order also on screen. How can I do that?

This is how the questions are referenced:

-(void)displayQuestion: (NSDictionary *) Question 
{
    [questionLabel setText:[Question objectForKey:@"Question"]];
    [answer1 setText:[Question objectForKey:@"Answer1"]];
    [answer2 setText:[Question objectForKey:@"Answer2"]];
    [answer3 setText:[Question objectForKey:@"Answer3"]]; 
}

<dict>
    <key>category</key>
    <string>Elementary Pilot</string>
    <key>Question</key>
    <string>You are approaching a hang glider head-on and at approximately the same height. You should:</string>
    <key>Answer1</key>
    <string>Turn to your right?</string>
    <key>Answer2</key>
    <string>Turn to your left?</string>
    <key>Answer3</key>
    <string>Lose height rapidly?</string>
</dict>

Use the category found in this answer What's the Best Way to Shuffle an NSMutableArray?

Put your answers in a NSMutableArray then shuffle the answers with

NSMutableArray *answers = //get answers
[answers shuffle];

If each question has only one answer, then you could simply keep two arrays, one for questions and one for answers. Obtain the count for answers and obtain a random number within that range using

int randomNumber = arc4random()%[answers count];

Then you can simply obtain the object in answers array at index = randomNumber.


Ok, create a questionObject that will have the question itself and an answerArray. When you read the plist you must go through the array of answers for each question and then you can simply follow either previous response from that point.


Ok, let's start from the beginning. What type of object is your plist? Hint, hint, a dictionary. Ok so far? to read a plist, you can find it's path first:

// Path to the plist (in the application bundle)
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"NameOfPlist" ofType:@"plist"];

Then just find the given dictionary, in this case plistInstance is a dictionary instance.

plistInstance = [[DataCacheManager dictionaryForPlistAtPath:plistPath] retain];

From there you will be able to access the direct keys for that plist. Which in that case it might be a questions array, so you can have something like this:

NSArray *questionsArray = [NSArray arrayWithArray:[plistInstance objectForKey:@"Questions"]];

Inside that array you should have your questions objects with a question string and an array for the answers to that question, so on so forth.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM