NSMutableArray can't be added to

Posted by Dan Ray on Stack Overflow See other posts from Stack Overflow or by Dan Ray
Published on 2010-05-17T14:08:46Z Indexed on 2010/05/17 14:21 UTC
Read the original article Hit count: 364

Filed under:
|
|

I've had this sort of problem before, and it didn't get a satisfactory answer.

I have a viewcontroller with a property called "counties" that is an NSMutableArray. I'm going to drill down a navigation screen to a view that is about selecting the counties for a geographical search. So the search page drills down to the "select counties" page.

I pass NSMutableArray *counties to the second controller as I push the second one on the navigation stack. I actually set that second controller's "selectedCounties" property (also an NSMutableArray) with a pointer to my first controller's "counties", as you'll see below.

When I go to addObject to that, though, 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 (the point here is, we're toggling each element of a table being active or not in 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 die at [self.selectedCounties addObject.... there.

Now, when I NSLog myself [self.selectedCounties class], it tells me it's an NSCFArray.

How does this happen? I understand about class bundles (or I THINK I do anyway), but this is explicitly a specific type, and it's losing it subclassing at some point in a way that kills the whole thing. I just completely don't understand why that would happen.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c