开发者

obj-c problem setting array with componentsSeperatedByString

开发者 https://www.devze.com 2022-12-26 23:19 出处:网络
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

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?


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消