1
votes

I have an NSMutableArray that did its initialisation like follows:

@interface Countries ()
{
    NSMutableArray *arrayofCountry;
}
@end


- (void)viewDidLoad
{
       //...
       arrayofCountry=[[NSMutableArray alloc]init];
       //...
}

Then I wanted to apply a removeObjectAtIndex to that NSMutableArray:

[arrayofCountry removeObjectAtIndex:sourceRow];

The problem is it is crashing with that log:

-[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance

I verified it with:

NSLog( NSStringFromClass( [arrayofCountry class] ));

and it's returning __NSArrayI. The question is how it was converted to NSArray? I'm just populating it with:

arrayofCountry=[JSON valueForKeyPath:@"country_name"];

Thank you for your help.

3
arrayofCountry = [NSMutableArray array]; - Desdenova
The problem lies in your last code bit. arrayOfCountry = [JSON valueForKeyPath:@"country_name"]; does NOT populate it. It replaces it. - Rey Gonzales

3 Answers

4
votes

Try this. This will create a new NSMutableArray based on the contents of your other array.

arrayofCountry = [NSMutableArray arrayWithArray:[JSON valueForKeyPath:@"country_name"]];
2
votes

If the value stored in the JSON file with the key country_name is an NSArray then it can't be copied into an NSMutableArray without changing it. Try doing this:

arrayofCountry = [[JSON valueForKeyPath:@"country_name"] mutableCopy];

Also, when creating your NSMutableArray create it like this (instead of using alloc and then init):

arrayOfCountry = [NSMutableArray array];
1
votes

try like this,

arrayofCountry=(NSMutableArray *)[JSON valueForKeyPath:@"country_name"];

as @ Ahmed Mohammed:suggested try like this way

arrayofCountry=[[JSON valueForKeyPath:@"country_name"] mutableCopy];