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.
My guess is that you're not allocating the array properly (e.g., NSMutableArray *arr = [[NSArray alloc] init]
, or are assigning an NSArray
to the NSMutableArray
variable. Can you post the code where you initialize the array?
Where do you init the object you set as counties? Maybe you did a mistake like:
NSMutableArray *counties = [[NSMutableArray alloc] init];
In this case no compile error will pop up but you cannot take changes on an array created like that!
来源:https://stackoverflow.com/questions/2849686/nsmutablearray-cant-be-added-to