0

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>
Justin Boo
  • 10,132
  • 8
  • 50
  • 71
  • How do you currently retrieve all the answers for one question from your array of dictionaries? – Phillip Mills Jun 20 '12 at 19:32
  • Sorry how to post code in the correct format please and I will show you. – Luke Rowland Jun 20 '12 at 19:46
  • You have to edit the question and add it there for it to show up well. (I looked at what you posted and it gets the question dictionary but doesn't show how the answers are referenced.) – Phillip Mills Jun 20 '12 at 19:49
  • OK, hopefully there now. – Luke Rowland Jun 20 '12 at 19:59
  • The idea below of making the answers into an array in the plist is probably best. Failing that, `NSMutableArray *answers = [NSMutableArray arrayWithObjects:[Question objectForKey:@"Answer1"], [Question objectForKey:@"Answer2"], [Question objectForKey:@"Answer3"], nil];` and randomizing `answers` could work. – Phillip Mills Jun 20 '12 at 20:46

2 Answers2

1

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];
Community
  • 1
  • 1
Scott Bossak
  • 2,471
  • 20
  • 17
0

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.

A Salcedo
  • 6,378
  • 8
  • 31
  • 42
  • How to get the answers from the plist into an array? Or do I have to start over? – Luke Rowland Jun 20 '12 at 20:05
  • I put that in and got a whole bunch of errors, I'll persevere, thanks for trying though. – Luke Rowland Jun 21 '12 at 22:55
  • I'm unclear about the line plistInstance = [[DataCacheManager dictionaryForPlistAtPath:plistPath] retain]; I can't get DataCacheManager to be highlighted, like yours. For starters :) – Luke Rowland Jun 24 '12 at 01:08
  • By plistInstance I mean an instance of an NSDictionary to hold the values from the property list. Then: NSDictionary *plistInstance = [[DataCacheManager dictionaryForPlistAtPath:plistPath] retain]; – A Salcedo Jun 26 '12 at 20:09
  • I'm getting the error 'Use of undeclared identifier 'DataCacheManager' – Luke Rowland Jun 29 '12 at 21:30