我以前有過這樣的問題,它沒有得到滿意的答案。NSMutableArray不能添加到
我有一個名爲「縣」屬性的視圖控制器是一個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。
這是怎麼發生的?我理解類捆綁(或者我認爲我是這麼做的),但是這是明確的特定類型,並且在某種程度上失去了它的子類化,從而殺死了整個事物。我完全不明白爲什麼會發生。
你如何創建你的counti es對象? – Vladimir 2010-05-17 14:11:48
你在哪裏分配'縣'? – kennytm 2010-05-17 14:12:33