Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

appiciux

macrumors newbie
Original poster
Sep 14, 2012
3
0
I am trying to make a random quiz app for the iPhone. I don't know about how to do that. There will be several questions, and I don't want to build a separate view for each one. What format I should import the questions in to the app, and then set it up to do questions in random order, and by category?
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
I am trying to make a random quiz app for the iPhone. I don't know about how to do that. There will be several questions, and I don't want to build a separate view for each one. What format I should import the questions in to the app, and then set it up to do questions in random order, and by category?

There are lots of ways to handle this. Here's one:



Create a dictionary object that has keys for the question, each possible answer, and the index of the correct answer (I'd make the list of possible answers an array inside the dictionary.)

Create an array of these dictionary objects. Each dictionary will be a quiz question and it's answers. The array will be an array of all possible quiz questions.

Save the array to a plist in your application bundle.

At launch, use NSArray arrayWithContentsOfFile to read the contents of your plist into an array of all your questions. Let's call that instance variable allQuizQuestions.

At the beginning of a quiz run, copy all the quiz question objects into a mutable array:

Code:
NSMutableArray *currentQuizQuestions = 
  [allQuizQuestions mutableCopy];

Then when you want to ask a question, get a random index into your current array of questions, fetch that question, and delete it from the array:

Code:
- (NSDictionary *) getRandomQuestion;
{
int count = [currentQuizQuestions count];
if (!count)
  return nil;
int randomIndex = arc4random_uniform(count);
NSDictionary *aQuestion = [currentQuizQuestions objectAtIndex: randomIndex];
[currentQuizQuestions removeObjectAtIndex: randomIndex];
return aQuestion;
}
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
Thank you, can i use core Data to store the question?

Sure, but do you know Core Data? Core data is a fairly advanced framework, and it takes a while to wrap your head around using it, even if you're an experienced iOS developer. If you're new to the platform, it is the wrong choice for something simple like this.

It's serious overkill for storing a list of quiz questions. A bit like taking a 747 to go to the convenience store across the street.
 

TouchMint.com

macrumors 68000
May 25, 2012
1,625
318
Phoenix
Thank you, can i use core Data to store the question?

I am also currently working on building a framework for a series of quizzes. I did so with core data because I know it pretty well (tbh never used a plist). Turns out you have to do pretty much all of the fetching and storing in arrays like duncan explained and it gets even more complicated when you need to store new data for questions (such as marking question for further review). Id say start simple and if you know plists then go for it. Feature creep is killing me with this quiz framework.
 

linnxiu

macrumors newbie
Sep 18, 2012
2
0
Thank you, can i use core Data to store the question?

Yes, you could store the questions using CoreData and read them from a JSON file or similar when the app starts. This will allow you to update the questions via a REST api

If you would use a DB I'd suggest to organize the questions in this way so it would be simple to use a single view and access the questions with SQL.

Code:
Q Table
QID | Q                              | Answer       | Category
1   | What is the capital of France? | Paris        | geo
2   | What is 7 + 2                  | 5            | math

I suggest also to check this source code, it could be useful if you are not so confident in developing: http://www.chupamobile.com/products/details/628/Quiz+Maker+Starter+Kit/
 

appiciux

macrumors newbie
Original poster
Sep 14, 2012
3
0
Yes, you could store the questions using CoreData and read them from a JSON file or similar when the app starts. This will allow you to update the questions via a REST api

If you would use a DB I'd suggest to organize the questions in this way so it would be simple to use a single view and access the questions with SQL.

Code:
Q Table
QID | Q                              | Answer       | Category
1   | What is the capital of France? | Paris        | geo
2   | What is 7 + 2                  | 5            | math

I suggest also to check this source code, it could be useful if you are not so confident in developing: http://www.chupamobile.com/products/details/628/Quiz+Maker+Starter+Kit/

Thank you for your suggestions, and the source code seems very usefull!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.