简体   繁体   English

在Objective-C中从文本文件读取字符串

[英]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). 截至目前,我拥有带有多种情况的switch语句,这些情况选择了图片(问题)和字符串(答案)。 Basically I don't want to keep all the strings in code in a .m file. 基本上,我不想将所有字符串保留在.m文件中。

The question will be in a form of a picture and a text file will hold answers. 问题将以图片的形式出现,而文本文件将包含答案。

answers.txt answers.txt

gta
fifa
minecraft

Questions: gta.jpg fifa.jpg minecraft.jpg 问题: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) 因此,随机化器将例如选择答案gta,并在选择答案时选择正确的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 所以最后看起来像这样:gta.jpg和四个答案选项,包括gta和播放器将选择正确的答案

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. 我认为Caleb的建议很好。 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. 您可以编写一次解析器方法,该方法将文本文件作为输入并生成输出plist。 I'm thinking your file would be multiple lines of imageNameanswer. 我认为您的文件应该是多行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. 然后,您将读取文本文件,使用NSString方法componentsSeparatedByString通过换行将其拆分为几行,然后再次使用componentsSeparatedByString:@“ \\ i”(制表符)循环遍历这些行将其转换为文件名和答案字符串。 You turn the results into an array of dictionaries and write it out to your app's documents folder. 您将结果转换为字典数组,并将其写到应用程序的documents文件夹中。 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. 如果只有少数几个,则plist或dictionary方法将起作用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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