NSMutableArray无法添加到 [英] NSMutableArray can't be added to
问题描述
我以前过这样的问题,并没有得到满意的答复。
我有一个名为 县,也就是一个NSMutableArray特性的视图控制器。我要去导航屏幕向下的观点,即是有关选择县的地理搜索。因此,在搜索页面向下钻取到 选择县 页面。
我通过的NSMutableArray *县
到第二控制器作为我推导航堆栈上的第二个。其实我设置第二个控制器的selectedCounties属性(也一个NSMutableArray)的指针我的第一个控制器的县,你会看到如下。
当我去 ADDOBJECT
来,虽然,我得到这样的:
***终止应用程序由于未捕获的异常 'NSInternalInconsistencyException',原因:*** - [NSCFArray insertObject:atIndex:]:突变方法发送到不变对象'
这是我的代码:
在SearchViewController.h:
@interface SearchViewController:的UIViewController
{
....
NSMutableArray的*县;
}
....
@属性(非原子,保留)的NSMutableArray *县;
在SearchViewController.m:
- (无效)getLocationsView
{
[keywordField resignFirstResponder];
SearchLocationsViewController * locationsController =
[[SearchLocationsViewController的alloc] initWithNibName:@ SearchLocationsView 束:无];
[self.navigationController pushViewController:locationsController动画:YES];
[locationsController setSelectedCounties:self.counties];
[locationsController释放];
}
在SearchLocationsViewController.h:
'pre>
@interface EventsSearchLocationsViewController:的UIViewController
将的UITableViewDelegate,UITableViewDataSource>
{
...
的NSMutableArray * selectedCounties;
}
...
@属性(非原子,保留)的NSMutableArray * selectedCounties;
在SearchLocationsViewController.m(这里的要点是,我们进行转换的表是活动的每个元素或不选择县列表):
- (无效)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath * )indexPath
{
的UITableViewCell *细胞= [的tableView的cellForRowAtIndexPath:indexPath];如果([self.selectedCounties containsObject:[self.counties objectAtIndex:indexPath.row]])
{
//我们deselcting!
[self.selectedCounties的removeObject:[self.counties objectAtIndex:indexPath.row]];
cell.accessoryView = [[ALLOC的UIImageView]
initWithImage:[UIImage的imageNamed:@ red_check_inactive.png]];
}
,否则{
[self.selectedCounties ADDOBJECT:[self.counties objectAtIndex:indexPath.row]];
cell.accessoryView = [[ALLOC的UIImageView]
initWithImage:[UIImage的imageNamed:@ red_check_active.png]];
}
[的tableView deselectRowAtIndexPath:indexPath动画:YES];
}
我们死在 [self.selectedCounties ADDOBJECT .. ..
那里。
现在,当我的NSLog自己 [self.selectedCounties类]
,它告诉我这是一个NSCFArray。
这是如何发生的呢?我了解类束(或我想我反正),但是这是一个明确的具体类型,并且它失去它在杀死了整个事情的方式某种程度上继承。我完全不明白为什么这会发生。
我的猜测是,你没有正确分配阵列(例如,的NSMutableArray * ARR = [[NSArray中的alloc] INIT]
,或者被分配一个的NSArray
到的NSMutableArray
变量。你能后,你初始化数组的代码?
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.
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?
这篇关于NSMutableArray无法添加到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!