NSMutableArray cannot be added to

I've had this problem before and didn't get a satisfactory answer.

I have a viewcontroller with a "counties" property that is an NSMutableArray. I'm going to expand the navigation screen to see how to select a graph for geographic search. So the search page is reduced to a "select countries" page.

I am passing NSMutableArray *counties

to the second controller when I push the second one in the navigation stack. I actually set this second "selectedCounties" property of the controller (also NSMutableArray) to point to my first "county" controller, as you will see below.

When I go to addObject

this, I get this:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'

      

Here's my code:

in SearchViewController.h:

@interface SearchViewController : UIViewController 
{
    ....
    NSMutableArray *counties;
}

....
@property (nonatomic, retain) NSMutableArray *counties;

      

in SearchViewController.m:

- (void)getLocationsView
{
    [keywordField resignFirstResponder];
    SearchLocationsViewController *locationsController = 
            [[SearchLocationsViewController alloc] initWithNibName:@"SearchLocationsView" bundle:nil];
    [self.navigationController pushViewController:locationsController animated:YES];
    [locationsController setSelectedCounties:self.counties];
    [locationsController release];
}

      

in SearchLocationsViewController.h:

@interface EventsSearchLocationsViewController : UIViewController 
    <UITableViewDelegate, UITableViewDataSource>
{
    ...
    NSMutableArray *selectedCounties;

}

...
@property (nonatomic, retain) NSMutableArray *selectedCounties;

      

in SearchLocationsViewController.m (here we translate each element of the active table or not to the list of selected counties):

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([self.selectedCounties containsObject:[self.counties objectAtIndex:indexPath.row]]) {
        //we're deselcting!
        [self.selectedCounties removeObject:[self.counties objectAtIndex:indexPath.row]];
        cell.accessoryView = [[UIImageView alloc]
                              initWithImage:[UIImage imageNamed:@"red_check_inactive.png"]];
    }
    else {
        [self.selectedCounties addObject:[self.counties objectAtIndex:indexPath.row]];
        cell.accessoryView = [[UIImageView alloc]
                              initWithImage:[UIImage imageNamed:@"red_check_active.png"]];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

      

We are dying at [self.selectedCounties addObject....

there.

Now that I am NSLog myself [self.selectedCounties class]

, it tells me that it is an NSCFArray.

How does this happen? I understand as far as class packages are concerned (or I THINK I don't care), but it's clearly a concrete type and it strips it out of subclassing at some point, which kills the whole thing. I just don't understand why this will happen.

+2


a source to share


2 answers


I am assuming that you are not allocating the array correctly (for example, NSMutableArray *arr = [[NSArray alloc] init]

or assigning to a NSArray

variable NSMutableArray

. Can you post the code where you initialize the array?



+4


a source


Where do you start the object that you set as circles? You may have made a mistake, for example:

NSMutableArray *counties = [[NSMutableArray alloc] init];

      



In this case, the compilation error will not appear, but you cannot make changes to the array created this way!

+1


a source







All Articles