obj-c问题设置数组与componentsSeparatedByString
我有一个大约 2000 行的数据源,如下所示:
6712,Anaktuvuk Pass Airport,Anaktuvuk Pass,United States,AKP,PAKP,68.1336,-151.743,2103,-9,A
我感兴趣的是第 6 个这个字符串的部分,所以我想把它变成一个数组,然后我想检查第 6 部分 [5] 是否出现该字符串“PAKP”
代码:
NSBundle *bundle = [NSBundle mainBundle];
NSString *airportsPath = [bundle pathForResource:@"airports" ofType:@"dat"];
NSData *data = [NSData dataWithContentsOfFile:airportsPath];
NSString *dataString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
NSArray *dataArray = [dataString componentsSeparatedByString:@"\n"];
NSRange locationOfAirport;
NSString *workingString = [[NSString alloc]initWithFormat:@""];
NSString *searchedAirport = [[NSString alloc]initWithFormat:@""];
NSString *airportData = [[NSString alloc]initWithFormat:@""];
int d;
for (d=0; d < [dataArray count]; d=d+1) {
workingString = [dataArray objectAtIndex:d];
testTextBox = workingString; //works correctly
NSArray *workingArray = [workingString componentsSeparatedByString:@","];
testTextBox2 = [workingArray objectAtIndex: 0]; //correctly displays the first section "6712"
testTextBox3 = [workingArray objectAtIndex:1] //throws exception index beyond bounds
locationOfAirport = [[workingArray objectAtIndex:5] rangeOfString:@"PAKP"];
}
问题是,当工作数组填充时,它只填充一个单个对象(字符串的第一个组成部分是“6712”。如果我让它显示工作字符串,它会正确显示整个字符串,但由于某种原因,它不能正确使用逗号创建数组。
我尝试过不使用数据文件并且工作正常,所以问题来自于我如何导入数据的
想法?
I have a data source with about 2000 lines that look like the following:
6712,Anaktuvuk Pass Airport,Anaktuvuk Pass,United States,AKP,PAKP,68.1336,-151.743,2103,-9,A
What I am interested in is the 6th section of this string so I want to turn it into an array, then i want to check the 6th section [5] for an occurrance of that string "PAKP"
Code:
NSBundle *bundle = [NSBundle mainBundle];
NSString *airportsPath = [bundle pathForResource:@"airports" ofType:@"dat"];
NSData *data = [NSData dataWithContentsOfFile:airportsPath];
NSString *dataString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
NSArray *dataArray = [dataString componentsSeparatedByString:@"\n"];
NSRange locationOfAirport;
NSString *workingString = [[NSString alloc]initWithFormat:@""];
NSString *searchedAirport = [[NSString alloc]initWithFormat:@""];
NSString *airportData = [[NSString alloc]initWithFormat:@""];
int d;
for (d=0; d < [dataArray count]; d=d+1) {
workingString = [dataArray objectAtIndex:d];
testTextBox = workingString; //works correctly
NSArray *workingArray = [workingString componentsSeparatedByString:@","];
testTextBox2 = [workingArray objectAtIndex: 0]; //correctly displays the first section "6712"
testTextBox3 = [workingArray objectAtIndex:1] //throws exception index beyond bounds
locationOfAirport = [[workingArray objectAtIndex:5] rangeOfString:@"PAKP"];
}
the problem is that when the workingArray populates, it only populates with a single object (the first component of the string which is "6712". If i have it display the workingString, it correctly displays the entire string, but for some reason, it isn't correctly making the array using the commas.
i tried it without using the data file and it worked fine, so the problem comes from how I am importing the data.
ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的代码有效。您应该使用调试器运行它以查看发生了什么。据猜测,您的输入数据并不是您想象的那样 - 可能是不同的编码或不同的行结尾。
参见示例:
You code works. You should run it with the debugger to see what's happening. At a guess, your input data isn't what you think it is - possibly a different encoding, or different line endings.
See sample:
数据中存在问题,其中有几个“\”导致了错误。
there was a problem in the data where there were a few "\"s that caused the errors.