2010-05-17 105 views
2

我以前有过这样的问题,它没有得到满意的答案。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。

这是怎么发生的?我理解类捆绑(或者我认为我是这么做的),但是这是明确的特定类型,并且在某种程度上失去了它的子类化,从而杀死了整个事物。我完全不明白为什么会发生。

+0

你如何创建你的counti es对象? – Vladimir 2010-05-17 14:11:48

+0

你在哪里分配'县'? – kennytm 2010-05-17 14:12:33

回答

4

我的猜测是,你没有正确分配阵列(例如,NSMutableArray *arr = [[NSArray alloc] init],或者被分配一个NSArrayNSMutableArray变量。你能后的代码,你初始化数组?

+1

有罪指控: 'self.counties = [[NSArray alloc] initWithObjects:@“Current Location”,nil];' 让我解决这个问题,看看事情是否突然发生。 – 2010-05-17 14:40:16

+0

宾果。问题是,我意识到,因为我在编码,所以我需要变得可变,而且我没有回去修复它需要修复的地方,我想。我来自那些事情已经变化的语言,所以这个区别对我来说并不是很自然。谢谢!! – 2010-05-17 14:43:39

1

你在哪里初始化?你设置为县对象也许你不喜欢一个错误:

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

在这种情况下,没有编译错误,就会弹出,但你不能把这样创建的阵列上的变化

+0

谢谢 - @ eman打了一两分钟就打败了你,但这是正确的答案。 – 2010-05-17 14:44:19