2015-01-08 120 views
0

我一直在嘗試將基於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

當然,我不是要求你爲我解決整個問題,也許只是儘可能將我推向正確的方向:)

回答

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]; 
} 
+0

驚人!非常感謝你!對此,我真的非常感激! :)最後我可以繼續我的項目。 –

+0

@ user243318:不客氣:)快樂編碼!!! –