私は以前にこの種の問題を抱えていましたが、満足のいく答えが得られませんでした。
NSMutableArray である「郡」と呼ばれるプロパティを持つビューコントローラーがあります。ナビゲーション画面をドリルダウンして、地理的検索のために郡を選択するビューに移動します。したがって、検索ページは「郡の選択」ページにドリルダウンします。
NSMutableArray *counties
2 番目のコントローラーをナビゲーション スタックにプッシュするときに、2 番目のコントローラーに渡します。以下に示すように、実際には、最初のコントローラーの「郡」へのポインターを使用して、2 番目のコントローラーの「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....
。
今、自分[self.selectedCounties class]
で NSLog を実行すると、NSCFArray であることがわかります。
これはどのように起こりますか?クラスバンドルについては理解していますが(またはとにかく理解していると思います)、これは明示的に特定のタイプであり、ある時点でサブクラス化してすべてを殺す方法で失われています。なぜそれが起こるのか、私にはまったく理解できません。