Random iPhone Quiz application

Discussion in 'iOS Programming' started by appiciux, Sep 14, 2012.

  1. appiciux macrumors newbie

    Sep 14, 2012
    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?
  2. Duncan C macrumors 6502a

    Duncan C

    Jan 21, 2008
    Northern Virginia
    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;
  3. appiciux thread starter macrumors newbie

    Sep 14, 2012
    Thank you, can i use core Data to store the question?
  4. Duncan C macrumors 6502a

    Duncan C

    Jan 21, 2008
    Northern Virginia
    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.
  5. TouchMint.com macrumors 68000


    May 25, 2012
    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.
  6. linnxiu macrumors newbie

    Sep 18, 2012
    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/
  7. appiciux thread starter macrumors newbie

    Sep 14, 2012
    Thank you for your suggestions, and the source code seems very usefull!

Share This Page