Namhcir
Namhcir

Reputation: 815

iOS file parsing

I've been asked to develop a multiple-choice-test iPhone app. The test has six categories, each category has maybe 25 to 30 questions. The user selects a category and then is presented which the questions, one at a time. Each question includes 4 possible answers; there is a brief explanation provided if the user selects the wrong one. I'm working off of a word doc (I guess it could be a text doc or even a pdf) that looks like the standard type of multiple-choice test we've all taken. Any advice as to how to read, parse and display the questions and answers? Reading through the various Guides, how-to's, sample code, etc., it's not at all obvious (at least not to me) how to get it done. I'm sure it's obvious to all of you that I'm rather new at this. Any suggestions would be greatly appreciated.

Upvotes: 0

Views: 2923

Answers (3)

Jim
Jim

Reputation: 73936

I'm making a few assumptions here:

  • The questions aren't expected to change regularly.
  • You are the only person that needs to edit the questions.
  • The design/layout doesn't change from question to question.

Given those assumptions, using a plist is probably the best approach for storing the questions.

Add a new file to your project, under Mac OS X > Resource you will find Property List. Don't worry, OS X and iOS property lists are the same.

When you open this file, you will see that you can create arrays, dictionaries, string values, etc. Your exact format will depend on your app, but roughly speaking:

  • Your top level object will be an array of categories
  • Your categories will be a dictionary containing a title string and an array of questions.
  • Your questions will be a dictionary containing a question, hint, and array of possible answers.
  • Your answers will be a dictionary containing the text and whether it is right or wrong.

From there, you can load all of your data with something like the following:

NSString *path = [[NSBundle mainBundle] pathForResource:@"questions" ofType:@"plist"];
NSArray *categories = [NSArray arrayWithContentsOfFile:path];

The categories object is a normal array and will reflect the contents of your plist. So, for example, to loop through categories and log the names, you'd do:

for (NSDictionary *category in categories) {
    NSLog(@"%@", [category objectForKey:@"Title"]);
}

Where you go from there depends on exactly how you want your user interface to work, but using a UINavigationController is probably a good start if you are completely stuck.

Upvotes: 2

Reno
Reno

Reputation: 33782

Im not sure whats your question is

Here is how to read a text file line by line.

Or reading a pdf as text. There are plenty of options out there. You should start coding and find resources as you proceed.

Upvotes: 1

Oscar Del Ben
Oscar Del Ben

Reputation: 4515

If working with an xml file is an option, go with that, in alternative you may want to take a look at yaml which offers a nice syntax. A more laborious alternative would be to set up a web application for your customer to submit questions and then to generate the xml directly from there.

Upvotes: 0

Related Questions