我一直在尝试将基于Mac OS的测验应用程序转换为iOS,因为我喜欢从单个.txt文件加载所有问题的想法。从.txt文件加载文本iOS
我在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
当然,我不是要求你为我解决整个问题,也许只是尽可能将我推向正确的方向:)
惊人!非常感谢你!对此,我真的非常感激! :)最后我可以继续我的项目。 –
@ user243318:不客气:)快乐编码!!! –