NSMutableArray 无法添加到
我以前也遇到过这样的问题,但没有得到满意的答案。
我有一个视图控制器,它有一个名为“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是您没有正确分配数组(例如
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 anNSArray
to theNSMutableArray
variable. Can you post the code where you initialize the array?您在哪里初始化设置为县的对象?也许您犯了这样的错误:
在这种情况下,不会弹出编译错误,但您无法对这样创建的数组进行更改!
Where do you init the object you set as counties? Maybe you did a mistake like:
In this case no compile error will pop up but you cannot take changes on an array created like that!