从.txt文件iOS加载文本

时间:2015-01-08 07:50:27

标签: ios objective-c text-files

我一直在尝试将基于Mac OS的测验应用转换为iOS,因为我喜欢从单个.txt文件加载所有问题的想法。

我之前仍然使用Objective-C语言,因为我之前使用过C#。

通过此功能加载问题和答案:

- (void)loadQuestionsAndAnswersArray {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Quiz1" ofType:@"txt"];
    NSString *textFileString = [NSString stringWithContentsOfFile:filePath encoding:NSStringEncodingConversionAllowLossy error:NULL];

    NSArray *seperatedQA = [textFileString componentsSeparatedByString:@"\n\n"];

    for (NSString *QA in seperatedQA) {
        NSString *questionString = [[[QA componentsSeparatedByString:@"\n"] objectAtIndex:0] stringByReplacingOccurrencesOfString:@"Q:" withString:@""];

        NSMutableArray *answers = [[QA componentsSeparatedByString:@"A:"] mutableCopy];
        [answers removeObjectAtIndex:0];

        int correctAnswerLoc = 0;

        for (int i = 0; i < answers.count; i++) {
            NSString *answer = [answers objectAtIndex:i];
            NSString *editedAnswer = [answer stringByReplacingOccurrencesOfString:@"\n" withString:@""];
            editedAnswer = [editedAnswer stringByTrimmingCharactersInSet:
                            [NSCharacterSet whitespaceAndNewlineCharacterSet]];
            [answers removeObjectAtIndex:i];
            [answers insertObject:editedAnswer atIndex:i];
            answer = editedAnswer;
            if ([answer rangeOfString:@"[CORRECT]"].location != NSNotFound) {
                correctAnswerLoc = [answers indexOfObject:answer];
                NSString *editedAnswer = [answer stringByReplacingOccurrencesOfString:@"[CORRECT]" withString:@""];
                [answers removeObjectAtIndex:i];
                [answers insertObject:editedAnswer atIndex:i];
            }
        }

        NSLog(@"answers = %@", answers);

        NSDictionary *QADictionary = [NSDictionary dictionaryWithObjectsAndKeys:questionString, @"question", answers, @"answers", [NSNumber numberWithInt:correctAnswerLoc], @"correctAnswerLocation", nil];

        [questionsAndAnswers addObject:QADictionary]; 
    }   

    resultsArray = [[NSMutableArray alloc] initWithCapacity:[questionsAndAnswers count]];
}

该应用程序然后有一个问题的文本字段,然后有3个按钮,每个答案一个。当出现新问题时,它会更改文本字段中的文本和按钮的标题。

此代码就像Mac App上的魅力一样,但在iOS版本上,它无法找到txt文件,按钮等会留空。

我现在已经坐了一个星期左右,这就是这篇文章的原因。

iOS应用程序基于此Github Mac应用程序:https://github.com/SquaredTiki/Quizzer

如果您想了解我之所以如何尝试转换应用,请点击此链接: https://www.dropbox.com/s/1jqz9ue97p3v2h1/iTrafikk.zip?dl=0

当然,我并没有要求你为我解决整个问题,如果可能的话,也许只是让我朝着正确的方向前进:)

1 个答案:

答案 0 :(得分:0)

我检查了你的项目。而且您似乎没有在代码中的任何位置调用loadQuestionsAndAnswersArray。您也没有在项目的任何位置初始化questionsAndAnswers

更改您的代码,如:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadQuestionsAndAnswersArray];
}

- (void)loadQuestionsAndAnswersArray
{
    NSString *filePath       = [[NSBundle mainBundle] pathForResource:@"Quiz1" ofType:@"txt"];
    NSString *textFileString = [NSString stringWithContentsOfFile:filePath encoding:NSStringEncodingConversionAllowLossy error:NULL];
    NSArray *seperatedQA     = [textFileString componentsSeparatedByString:@"\n\n"];

    for (NSString *QA in seperatedQA)
    {
        NSString *questionString = [[[QA componentsSeparatedByString:@"\n"] objectAtIndex:0] stringByReplacingOccurrencesOfString:@"Q:" withString:@""];
        NSMutableArray *answers  = [[QA componentsSeparatedByString:@"A:"] mutableCopy];

        [answers removeObjectAtIndex:0];

        int correctAnswerLoc = 0;

        for (int i = 0; i < answers.count; i++)
        {
            NSString *answer       = [answers objectAtIndex:i];
            NSString *editedAnswer = [answer stringByReplacingOccurrencesOfString:@"\n" withString:@""];
            editedAnswer           = [editedAnswer stringByTrimmingCharactersInSet:
                            [NSCharacterSet whitespaceAndNewlineCharacterSet]];
            [answers removeObjectAtIndex:i];
            [answers insertObject:editedAnswer atIndex:i];
            answer = editedAnswer;
            if ([answer rangeOfString:@"[CORRECT]"].location != NSNotFound)
            {
                correctAnswerLoc       = [answers indexOfObject:answer];
                NSString *editedAnswer = [answer stringByReplacingOccurrencesOfString:@"[CORRECT]" withString:@""];
                [answers removeObjectAtIndex:i];
                [answers insertObject:editedAnswer atIndex:i];
            }
        }

        NSLog(@"answers = %@", answers);

        NSDictionary *QADictionary = [NSDictionary dictionaryWithObjectsAndKeys:questionString, @"question", answers, @"answers", [NSNumber numberWithInt:correctAnswerLoc], @"correctAnswerLocation", nil];
        if (!questionsAndAnswers)
        {
            questionsAndAnswers = [[NSMutableArray alloc] init];
        }
        [questionsAndAnswers addObject:QADictionary]; 
    }   

    resultsArray = [[NSMutableArray alloc] initWithCapacity:[questionsAndAnswers count]];
    [self loadQuestionsAndAnswersIntoInterface];
}