gpt4 book ai didi

iphone - 为什么我的NSString无法读取我的.txt文件?

转载 作者:行者123 更新时间:2023-12-01 17:15:11 28 4
gpt4 key购买 nike

我有以下代码读取.txt文件并将其放入用于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);

但是,当按下按钮使所有这些发生时,所有字符串(空)。每个文本文件
应该可以完全正常工作,因为它们已经为我的应用程序的其他部分工作了。

有没有更好的方法应该这样做,还是可以改进当前方法以获得所需的结果?

谢谢

最佳答案

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

适当替换编码和错误值。

关于iphone - 为什么我的NSString无法读取我的.txt文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8347045/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com