androniennn
androniennn

Reputation: 3137

NSMutableArray always converting to NSArray

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.

Upvotes: 1

Views: 272

Answers (3)

Sam Spencer
Sam Spencer

Reputation: 8607

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

Upvotes: 2

Mark McCorkle
Mark McCorkle

Reputation: 9414

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

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

Upvotes: 4

Balu
Balu

Reputation: 8460

try like this,

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

as @ Ahmed Mohammed:suggested try like this way

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

Upvotes: 1

Related Questions