obj-c problem setting array with componentsSeperatedByString

Posted by Brodie4598 on Stack Overflow See other posts from Stack Overflow or by Brodie4598
Published on 2010-04-10T16:13:52Z Indexed on 2010/04/10 16:43 UTC
Read the original article Hit count: 400

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about arrays

Related posts about objective-c