Question

I have an NSMutableArray *myArray, wich is the result of a properly .xml parsed file, using TouchXML.

I just want to extract all elements with key name and store then in a separate NSMutableArray, but my final NSMutableArray *namesList is not accessible, crashing my iPhone app right away, because it only contains the last enumerated string and not the whole array.

Here is the code:

NSMutableArray *namesList = [[NSMutableArray alloc] initWithArray:myArray copyItems:YES];

       int i;
        for (i = 0; i < [myArray count]; i++) 
           {
            namesList = [[myArray objectAtIndex:i] objectForKey:@"name"];
            NSLog(@"All Names: %@", namesList);    
           }

          NSLog(@"First Name: %@", [namesList objectAtIndex:0]); <-- crashing line

And here is the NSLog:

-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x6a42540
2012-04-04 10:34:07.882 Sections[3610:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x6a42540'
Was it helpful?

Solution

You should be able to do this:

NSMutableArray * namesList = [NSMutableArray alloc] init];

for(i = 0; i < [myArray count]; i++)
{ 

    NSString * tempString = [NSString stringWithString:[[myArray objectAtIndex:i] objectForKey:@"name"]];
    [namesList addobject:tempString];
}

 NSLog(@"First Name: %@", [namesList objectAtIndex:0]);

OTHER TIPS

[[myArray objectAtIndex:i] objectForKey:@"name"]; returns an NSString object and not another array therefore you can't send an objectAtIndex to it. Check the structure of your arrays.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top