obj-c问题设置数组与componentsSeparatedByString

发布于 2024-08-28 13:14:09 字数 1550 浏览 5 评论 0原文

我有一个大约 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红ご颜醉 2024-09-04 13:14:09

你的代码有效。您应该使用调试器运行它以查看发生了什么。据猜测,您的输入数据并不是您想象的那样 - 可能是不同的编码或不同的行结尾。

参见示例:

NSString *dataString = @"6712,Anaktuvuk Pass Airport,Anaktuvuk Pass,United States,AKP,PAKP,68.1336,-151.743,2103,-9,A";
NSArray *dataArray = [dataString componentsSeparatedByString:@"\n"];
for (NSString *workingString in dataArray) {
    NSString *testTextBox = workingString; //works correctly
NSArray *workingArray = [workingString componentsSeparatedByString:@","];
    NSString *testTextBox2 = [workingArray objectAtIndex: 0]; //correctly displays the first section "6712"
    NSString *testTextBox3 = [workingArray objectAtIndex:1]; //throws exception index beyond bounds
NSRange locationOfAirport = [[workingArray objectAtIndex:5] rangeOfString:@"PAKP"];
}

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:

NSString *dataString = @"6712,Anaktuvuk Pass Airport,Anaktuvuk Pass,United States,AKP,PAKP,68.1336,-151.743,2103,-9,A";
NSArray *dataArray = [dataString componentsSeparatedByString:@"\n"];
for (NSString *workingString in dataArray) {
    NSString *testTextBox = workingString; //works correctly
NSArray *workingArray = [workingString componentsSeparatedByString:@","];
    NSString *testTextBox2 = [workingArray objectAtIndex: 0]; //correctly displays the first section "6712"
    NSString *testTextBox3 = [workingArray objectAtIndex:1]; //throws exception index beyond bounds
NSRange locationOfAirport = [[workingArray objectAtIndex:5] rangeOfString:@"PAKP"];
}
过度放纵 2024-09-04 13:14:09

数据中存在问题,其中有几个“\”导致了错误。

there was a problem in the data where there were a few "\"s that caused the errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文