正在从 .txt 文件加载文本 iOS
Loading text from .txt file iOS
我一直在尝试将基于 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 应用程序上非常有用,但在 iOS 版本上它就像找不到 txt 文件,按钮等留空。
我已经坐了大约一个星期了,这就是 post 的原因。
iOS 应用基于此 Github Mac 应用:https://github.com/SquaredTiki/Quizzer
如果您想了解我是如何尝试将应用程序转换为以下 link 的:
https://www.dropbox.com/s/1jqz9ue97p3v2h1/iTrafikk.zip?dl=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];
}
我一直在尝试将基于 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 应用程序上非常有用,但在 iOS 版本上它就像找不到 txt 文件,按钮等留空。
我已经坐了大约一个星期了,这就是 post 的原因。
iOS 应用基于此 Github Mac 应用:https://github.com/SquaredTiki/Quizzer
如果您想了解我是如何尝试将应用程序转换为以下 link 的: https://www.dropbox.com/s/1jqz9ue97p3v2h1/iTrafikk.zip?dl=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];
}