1

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.

2
  • arrayofCountry = [NSMutableArray array]; Commented Jul 18, 2013 at 14:30
  • 1
    The problem lies in your last code bit. arrayOfCountry = [JSON valueForKeyPath:@"country_name"]; does NOT populate it. It replaces it. Commented Jul 18, 2013 at 14:46

3 Answers 3

4

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

arrayofCountry = [NSMutableArray arrayWithArray:[JSON valueForKeyPath:@"country_name"]];
Sign up to request clarification or add additional context in comments.

1 Comment

If you need a retained version you can do the same thing with [[NSMutablArray alloc] initWithArray: ]; If DCGoD's answer is correct, please mark it as such.
2

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

3 Comments

It worked too. So the problem is that I was getting an array from the JSON and applying it to an NSMutableArray.
@androniennn Correct! And that's why you were getting a crash, and the change in pointer type. Accept the answer that best resolves the problem and explains it. :-)
@androniennn Even though you overwrite later on, every other answer is missing this technically important bit arrayOfCountry = [NSMutableArray array];
1

try like this,

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

as @ Ahmed Mohammed:suggested try like this way

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

3 Comments

tried it right now and it's always crashing at removeObjectAtIndex.
are you getting same crash description?
casting the pointer does not change the underlying object's class!! Instead you should try arrayofCountry=[[JSON valueForKeyPath:@"country_name"] mutableCopy];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.