2011-12-01 129 views
1

我有以下代碼來讀取.txt文件並將其放入一個字符串用於NSScanner。爲什麼我的NSString不能讀取我的.txt文件?

NSString *Period1String = [[NSBundle mainBundle] pathForResource: @"Period1String" ofType: @"txt"]; 
    NSString *Period2String = [[NSBundle mainBundle] pathForResource: @"Period2String" ofType: @"txt"]; 
    NSString *Period3String = [[NSBundle mainBundle] pathForResource: @"Period3String" ofType: @"txt"]; 
    NSString *Period4String = [[NSBundle mainBundle] pathForResource: @"Period4String" ofType: @"txt"]; 
    NSString *Period5String = [[NSBundle mainBundle] pathForResource: @"Period5String" ofType: @"txt"]; 

    NSURL *requestTimetableURL = [NSURL URLWithString:@"http://www.dhsb.org/index.phtml?d=201435"]; 
    NSLog(@"Loaded Timetable"); 
    NSError *loaderror; 
    NSString *page = [NSString stringWithContentsOfURL:requestTimetableURL 
               encoding:NSASCIIStringEncoding 
               error:&loaderror]; 

    [webView loadHTMLString:page baseURL:requestTimetableURL]; 

    NSString* Period1; 

    NSScanner *htmlScanner = [NSScanner scannerWithString:page]; 

    [htmlScanner scanUpToString:Period1String intoString:NULL]; 
    [htmlScanner scanString:Period1String intoString:NULL]; 
    [htmlScanner scanUpToString:@"</FONT>" intoString:&Period1]; 

    period1label.text= Period1; 

    NSLog(@"Collected Period 1 Data: %@", Period1); 

    NSScanner *htmlScanner2 = [NSScanner scannerWithString:page]; 

    NSString* Period2; 

    [htmlScanner2 scanUpToString:Period2String intoString:NULL]; 
    [htmlScanner2 scanString:Period2String intoString:NULL]; 
    [htmlScanner2 scanUpToString:@"</FONT>" intoString:&Period2]; 

    period2label.text= Period2; 

    NSLog(@"Collected Period 2 Data: %@", Period2); 

    NSScanner *htmlScanner3 = [NSScanner scannerWithString:page]; 

    NSString* Period3; 

    [htmlScanner3 scanUpToString:Period3String intoString:NULL]; 
    [htmlScanner3 scanString:Period3String intoString:NULL]; 
    [htmlScanner3 scanUpToString:@"</FONT>" intoString:&Period3]; 

    period3label.text= Period3; 

    NSLog(@"Collected Period 3 Data: %@", Period3); 

    NSScanner *htmlScanner4 = [NSScanner scannerWithString:page]; 

    NSString* Period4; 

    [htmlScanner4 scanUpToString:Period4String intoString:NULL]; 
    [htmlScanner4 scanString:Period4String intoString:NULL]; 
    [htmlScanner4 scanUpToString:@"</FONT>" intoString:&Period4]; 

    period4label.text= Period4; 

    NSLog(@"Collected Period 4 Data: %@", Period4); 

    NSScanner *htmlScanner5 = [NSScanner scannerWithString:page]; 

    NSString* Period5; 

    [htmlScanner5 scanUpToString:Period5String intoString:NULL]; 
    [htmlScanner5 scanString:Period5String intoString:NULL]; 
    [htmlScanner5 scanUpToString:@"</FONT>" intoString:&Period5]; 

    period5label.text = Period5; 

    NSLog(@"Collected Period 5 Data: %@", Period5); 

但是,當按鈕被按下時,所有這些都會發生,所有的字符串(空)。每個文本文件 應該完全正常工作,因爲他們已經爲我的應用程序的其他部分工作。

有沒有更好的方法,我應該這樣做,或者我可以改進目前的這種方法來獲得我期待的結果?

感謝

+0

如果我爲Period1Label,Period2Label,Period3Label,Period4Label和Period5Label做一個NSLog語句 - 那麼這會發生什麼:http://pastie.org/2951134 – pixelbitlabs

回答

16
NSString *Period1String = [[NSBundle mainBundle] pathForResource: @"Period1String" ofType: @"txt"]; 

這是設置Period1String的路徑Period1String.txt而非Period1String.txt的內容。

它應該是

NSString *Period1String = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource: @"Period1String" ofType: @"txt"] usedEncoding:nil error:nil]; 

替換被用於編碼和誤差作爲相應的值。

+0

令人驚歎的謝謝!不知道,否則就像在互聯網上沒有說的那樣:D – pixelbitlabs

+0

只需再提一個問題;爲什麼這不起作用 - 這不是說「Period1」包含某些文本:http://pastie.org/2951250 – pixelbitlabs

+0

你應該真的問這是一個新問題,但作爲一個快速猜測,你必須使用[Period1 isEqualToString:@「Art] - 你不能使用==進行字符串比較,因爲它比較了對象地址,而不是字符串內容。 – jrturton

相關問題