nsmutablearrayを追加することはできません
-
27-09-2019 - |
質問
私は以前にこの種の問題を抱えていましたが、満足のいく答えが得られませんでした。
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'
これが私のコードです:
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 SeartLocationsViewController.H:
@interface EventsSearchLocationsViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource>
{
...
NSMutableArray *selectedCounties;
}
...
@property (nonatomic, retain) NSMutableArray *selectedCounties;
SeartLocationsViewController.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であると言っています。
これはどのように起こりますか?私はクラスのバンドルについて理解しています(または私はとにかく私はそうしていると思います)が、これは明示的に特定のタイプであり、全体を殺す方法である時点でサブクラス化を失っています。なぜそれが起こるのか完全にわかりません。
解決
私の推測では、あなたは配列を適切に割り当てていないということです(例: NSMutableArray *arr = [[NSArray alloc] init]
, 、またはANを割り当てています NSArray
に NSMutableArray
変数。配列を初期化するコードを投稿できますか?
他のヒント
郡として設定したオブジェクトはどこから入手しますか?多分あなたは次のような間違いをしました:
NSMutableArray *counties = [[NSMutableArray alloc] init];
この場合、コンパイルエラーはポップアップ表示されませんが、そのように作成された配列を変更することはできません!