2010-05-17 71 views
2

我以前有過這樣的問題,它沒有得到滿意的答案。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。

這是怎麼發生的?我理解類捆綁(或者我認爲我是這麼做的),但是這是明確的特定類型,並且在某種程度上失去了它的子類化,從而殺死了整個事物。我完全不明白爲什麼會發生。

+0

你如何創建你的counti es對象? – Vladimir 2010-05-17 14:11:48

+0

你在哪裏分配'縣'? – kennytm 2010-05-17 14:12:33

回答

4

我的猜測是,你沒有正確分配陣列(例如,NSMutableArray *arr = [[NSArray alloc] init],或者被分配一個NSArrayNSMutableArray變量。你能後的代碼,你初始化數組?

+1

有罪指控: 'self.counties = [[NSArray alloc] initWithObjects:@「Current Location」,nil];' 讓我解決這個問題,看看事情是否突然發生。 – 2010-05-17 14:40:16

+0

賓果。問題是,我意識到,因爲我在編碼,所以我需要變得可變,而且我沒有回去修復它需要修復的地方,我想。我來自那些事情已經變化的語言,所以這個區別對我來說並不是很自然。謝謝!! – 2010-05-17 14:43:39

1

你在哪裏初始化?你設置爲縣對象也許你不喜歡一個錯誤:

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

在這種情況下,沒有編譯錯誤,就會彈出,但你不能把這樣創建的陣列上的變化

+0

謝謝 - @ eman打了一兩分鐘就打敗了你,但這是正確的答案。 – 2010-05-17 14:44:19