我以前过这样的问题,并没有得到满意的答复。

我有一个名为“县”,也就是一个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。

这是如何发生的呢?我了解类束(或我想我反正),但是这是一个明确的具体类型,并且它失去它在杀死了整个事情的方式某种程度上继承。我完全不明白为什么这会发生。

有帮助吗?

解决方案

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

其他提示

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

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top