NSMutableArray 无法添加到

发布于 2024-09-01 11:36:33 字数 2751 浏览 4 评论 0原文

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

我有一个视图控制器,它有一个名为“counties”的属性,它是一个 NSMutableArray。我将深入导航屏幕,找到有关选择县进行地理搜索的视图。因此,搜索页面会深入到“选择县”页面。

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

但是,当我转到 addObject 时,我得到了: 这

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'

是我的代码:

in SearchViewController.h:

@interface SearchViewController : UIViewController 
{
    ....
    NSMutableArray *counties;
}

....
@property (nonatomic, retain) NSMutableArray *counties;

in 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];
}

in SearchLocationsViewController.h:

@interface EventsSearchLocationsViewController : UIViewController 
    <UITableViewDelegate, UITableViewDataSource>
{
    ...
    NSMutableArray *selectedCounties;

}

...
@property (nonatomic, retain) NSMutableArray *selectedCounties;

in 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。

这是怎么发生的?我了解类捆绑(或者我认为我无论如何都了解),但这显然是一种特定类型,并且它在某些时候失去了它的子类化,从而杀死了整个事情。我完全不明白为什么会发生这种情况。

I've had this sort of problem before, and it didn't get a satisfactory answer.

I have a viewcontroller with a property called "counties" that is an NSMutableArray. I'm going to drill down a navigation screen to a view that is about selecting the counties for a geographical search. So the search page drills down to the "select counties" page.

I pass NSMutableArray *counties to the second controller as I push the second one on the navigation stack. I actually set that second controller's "selectedCounties" property (also an NSMutableArray) with a pointer to my first controller's "counties", as you'll see below.

When I go to addObject to that, though, I get this:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'

Here's my code:

in SearchViewController.h:

@interface SearchViewController : UIViewController 
{
    ....
    NSMutableArray *counties;
}

....
@property (nonatomic, retain) NSMutableArray *counties;

in 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];
}

in SearchLocationsViewController.h:

@interface EventsSearchLocationsViewController : UIViewController 
    <UITableViewDelegate, UITableViewDataSource>
{
    ...
    NSMutableArray *selectedCounties;

}

...
@property (nonatomic, retain) NSMutableArray *selectedCounties;

in SearchLocationsViewController.m (the point here is, we're toggling each element of a table being active or not in the list of selected counties):

-(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];
}

We die at [self.selectedCounties addObject.... there.

Now, when I NSLog myself [self.selectedCounties class], it tells me it's an NSCFArray.

How does this happen? I understand about class bundles (or I THINK I do anyway), but this is explicitly a specific type, and it's losing it subclassing at some point in a way that kills the whole thing. I just completely don't understand why that would happen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏至、离别 2024-09-08 11:36:33

我的猜测是您没有正确分配数组(例如 NSMutableArray *arr = [[NSArray alloc] init],或者将 NSArray 分配给 NSMutableArray 变量。您可以发布初始化数组的代码吗?

My guess is that you're not allocating the array properly (e.g., NSMutableArray *arr = [[NSArray alloc] init], or are assigning an NSArray to the NSMutableArray variable. Can you post the code where you initialize the array?

看轻我的陪伴 2024-09-08 11:36:33

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

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

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

Where do you init the object you set as counties? Maybe you did a mistake like:

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

In this case no compile error will pop up but you cannot take changes on an array created like that!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文