views:

82

answers:

2

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?

+2  A: 

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"];
}
Paul Lynch
yes i also tried that. I know the problem is with how the data is being read, but I dont understand why, if I have it display workingString it displays the string, with the commas, but it cant see them when making the array. The line endings are okay because when i use [dataString componentsSeparatedByString:@"\n"]; it correctly separates each line, but the commas is whats throwing it off. Different encoding than what do you mean?
Brodie
i just tried replacing all the ","s with "|"'s and tried it that way. Same problem. For whatever reason it is only recognizing the first component.
Brodie
A: 

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

Brodie