简体   繁体   中英

Reading strings from a text file in Objective-C

The game is similar to the quiz game. Questions are pictures and answers are strings. Just wondering what would be the best way to read strings(answer) from the text file randomly, in order to use the string selected to pull up pictures(questions) from a set of pictures. Pictures will have the same names as all the name strings in the text file, however I can't have them repeat. As of now I have switch statement that has multiple cases that select the picture(question) and strings(answers). Basically I don't want to keep all the strings in code in a .m file.

The question will be in a form of a picture and a text file will hold answers.

answers.txt

gta
fifa
minecraft

Questions: gta.jpg fifa.jpg minecraft.jpg

so the randomizer will for example pick answer gta and when it does so, it should select the right pic(gta.jpg)

so at the end it will look like this: gta.jpg and four answers choices including the gta and the player will pick the right answer

is this clear?

Use a property list. Store the list of questions as an array of dictionaries, where each dictionary has entries for the question file name and the answer, like this:

[
    {
        "question" : "gta.jpg",
        "answer" : "gta"
    },
    {
        "question" : "fifa.jpg"
        "answer" : "fifa"
    },
    //...
]

Then you can read the dictionary into memory using a convenience method:

NSArray *questions = [NSArray arrayWithContentOfFile:pathToQuestionsPList];

I think Caleb's suggestion is very good. By having an array of dictionaries, string and it's image are always kept together.

You could write a one-time parser method that would take a text file as input and generate your output plist. I'm thinking your file would be multiple lines of imageNameanswer. You'd then read the text file, use the NSString method componentsSeparatedByString to break it up into lines by line break, and then loop through the lines, again using componentsSeparatedByString: @"\\i" (the tab character), this time to break it into a filename and an answer string. You turn the results into an array of dictionaries and write it out to your app's documents folder. Then just drag the result into your project.

If you wanted to get really fancy you could turn your text file parser into a command-line tool, and make it part of the build process so that when you update your text file of image names and answers, the build process automatically runs the parser on it and copies the output into the application bundle. Methinks that's a little beyond your current abilities however.

If you have a lot of questions you probably want to learn to use core data and a database. If you only have a few, then the plist or dictionary method will work.

One of my apps has a bunch of stories in a database and at the last minute, we decided to add images. Rather than messing with the database, I wrote a quick class that uses a dictionary to pair the story with the an image name.

The view controller queries the class to get the name of the image.

NSString *imageName = [EarlyReadingImageNames findStoryImage:title];

This is the full class.

//
//  EarlyReadingImageNames.m
//  Words
//
//  Created by John Scarry on 5/20/14.
//

#import "EarlyReadingImageNames.h"

@implementation EarlyReadingImageNames

+(NSString *) findStoryImage:(NSString *)story {

    NSDictionary* imageDictionary= @{ @"Alice the Worker Bee" : @"Alice",
                                      @"Alice Learns to Fly" : @"Alice",
                                      @"Alice Loves Her New Job" : @"Alice",
                                      @"George Likes to Sing" : @"George",
                                      @"George Likes to Dance" : @"George",
                                      @"George Saves the Day" : @"George",
                                      @"Jensen Meets Bob the Buffalo" : @"JensenBob",
                                      @"Jensen and Bob Play in the Pond" : @"JensenBob",
                                      @"Jensen and Bob Make a Pair of Boots" : @"JensenBob",
                                      @"Rita Finds a New Home" : @"Rita",
                                      @"Rita Makes a Boat" : @"Rita",
                                      @"Rita Loves Words" : @"Rita",
                                      @"The Rock That Looked Like a Frog" : @"Sandy",
                                      @"The Rock and the Rainbow" : @"Sandy",
                                      @"Sandy Makes New Friends" : @"Sandy",
                                      @"James and the Bowl of Baseballs" : @"James", 
                                      @"James and the Garden" : @"James", 
                                      @"James Builds a Bird House" : @"James", 
                                      @"Lily Finds Eggs" : @"Lily",
                                      @"Lily and Bessie the Cow" : @"Lily", 
                                      @"Lily Feeds the Lambs" : @"Lily", 
                                      @"Hector and Bo" : @"Hector", 
                                      @"Hector Loves Fish Socks" : @"Hector",
                                      @"Hector Makes a Kite" : @"Hector", 
                                      @"Yoshi and Toshiko Get a New Home" : @"ToshikoYoshi",
                                      @"Yoshi and Toshiko Go to the Library" : @"ToshikoYoshi",
                                      @"Yoshi and Toshiko Go to the Park" : @"ToshikoYoshi",
                                      @"Pete Loves Birds" : @"Pete",
                                      @"Pete Meets Max" : @"Pete", 
                                      @"Pete and Max Are Best Friends" : @"Pete"
                                      };

    return [imageDictionary valueForKey:story];
}

@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM