代码之家  ›  专栏  ›  技术社区  ›  Dan Ray

无法将NSMutableArray添加到

  •  2
  • Dan Ray  · 技术社区  · 16 年前

    我以前遇到过这种问题,但没有得到满意的答案。

    我通过了 NSMutableArray *counties 当我将第二个控制器推到导航堆栈上时。实际上,我设置了第二个控制器的“selectedCounties”属性(也是一个NSMutableArray),指针指向第一个控制器的“counties”,如下所示。

    当我去 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.... 在那里。

    现在,当我自己登录时 [self.selectedCounties class]

    这是怎么发生的?我理解类束(或者我想我无论如何也理解),但这是一个显式的特定类型,它在某个点上丢失了它的子类化,从而扼杀了整个过程。我完全不明白为什么会这样。

    2 回复  |  直到 16 年前
        1
  •  4
  •   shosti    16 年前

    NSMutableArray *arr = [[NSArray alloc] init] NSArray NSMutableArray 变量。你能把代码贴在初始化数组的地方吗?

        2
  •  1
  •   Community Mohan Dere    11 年前

    你在哪里初始化你设置为县的对象?也许你犯了这样的错误:

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

    在这种情况下,不会弹出编译错误,但不能对这样创建的数组进行更改!