Why is my NSString not reading my .txt file?

I have the following code to read a .txt file and to put it into a string to use for NSScanner.

    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);

But, when the button is pressed for all of this to happen, it all the strings (null). Every text file should be fully working as they have worked for other parts of my app.

Is there a better way I should be doing this or can I improve on this current method to get the results I am looking for?

Thanks

Answers


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

This is setting Period1String to the path to Period1String.txt rather than the contents of Period1String.txt.

It should be

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

Substitute in values for encoding and error as appropriate.


Need Your Help

Perl: Using a conditional with an array

arrays perl loops syntax conditionals

So I'll start of by saying I'm not very familiar with Perl. I have a project that I've been handed at work that requires quite a bit of Perl work. Most of it makes sense but I'm stuck on a very sim...

how to get perf to decode symbols for hadoop java code

java linux performance ubuntu hadoop

I'm using perf tools on ubuntu 12.04 to profile system level performance of hadoop 2.4.1 map/reduce jobs by doing:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.