我以前也遇到过这样的问题,但没有得到满意的答案。
我有一个视图控制器,它有一个名为“counties”的属性,它是一个 NSMutableArray。我将深入导航屏幕,找到有关选择县进行地理搜索的视图。因此,搜索页面会深入到“选择县”页面。
I pass NSMutableArray *counties
当我将第二个控制器推到导航堆栈上时,到第二个控制器。实际上,我使用指向第一个控制器的“counties”的指针设置了第二个控制器的“selectedCounties”属性(也是一个 NSMutableArray),如下所示。
当我去addObject
不过,对此我明白了:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
这是我的代码:
在搜索ViewController.h中:
@interface SearchViewController : UIViewController
{
....
NSMutableArray *counties;
}
....
@property (nonatomic, retain) NSMutableArray *counties;
在搜索ViewController.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];
}
在 SearchLocations ViewController.h 中:
@interface EventsSearchLocationsViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource>
{
...
NSMutableArray *selectedCounties;
}
...
@property (nonatomic, retain) NSMutableArray *selectedCounties;
在 SearchLocationsViewController.m 中(这里的要点是,我们要切换表中每个元素在选定县列表中是否处于活动状态):
-(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];
}
我们死在[self.selectedCounties addObject....
there.
现在,当我自己 NSLog 时[self.selectedCounties class]
,它告诉我这是一个 NSCFArray。
这是怎么发生的?我了解类捆绑(或者我认为我无论如何都了解),但这显然是一种特定类型,并且它在某些时候失去了它的子类化,从而杀死了整个事情。我完全不明白为什么会发生这种情况。