PDA

View Full Version : Random iPhone Quiz application




appiciux
Sep 14, 2012, 11:54 AM
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
Sep 14, 2012, 12:22 PM
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:

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:

- (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;
}

appiciux
Sep 18, 2012, 01:30 PM
Thank you, can i use core Data to store the question?

Duncan C
Sep 18, 2012, 02:20 PM
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
Sep 18, 2012, 04:09 PM
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
Sep 19, 2012, 04:58 AM
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.

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
Sep 19, 2012, 05:27 AM
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.

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!