我以前有过这样的问题,它没有得到满意的答案。NSMutableArray不能添加到
我有一个名为“县”属性的视图控制器是一个NSMutableArray。我打算将导航屏幕深入到一个关于选择地理搜索县的视图。因此,搜索页面将深入到“选择县”页面。
我将NSMutableArray *counties
传递给第二个控制器,因为我在导航堆栈上按下第二个控制器。我实际上用第一个控制器的“县”指针设置第二个控制器的“selectedCounties”属性(也是一个NSMutableArray),如下所示。
当我去到addObject
,虽然,我得到这个:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
这里是我的代码:
在SearchViewController.h:
@interface SearchViewController : UIViewController
{
....
NSMutableArray *counties;
}
....
@property (nonatomic, retain) NSMutableArray *counties;
在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];
}
在SearchLocationsViewController.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....
那里。
现在,当我NSLog自己[self.selectedCounties class]
,它告诉我这是一个NSCFArray。
这是怎么发生的?我理解类捆绑(或者我认为我是这么做的),但是这是明确的特定类型,并且在某种程度上失去了它的子类化,从而杀死了整个事物。我完全不明白为什么会发生。
你如何创建你的counti es对象? – Vladimir 2010-05-17 14:11:48
你在哪里分配'县'? – kennytm 2010-05-17 14:12:33