2

私は以前にこの種の問題を抱えていましたが、満足のいく答えが得られませんでした。

NSMutableArray である「郡」と呼ばれるプロパティを持つビューコントローラーがあります。ナビゲーション画面をドリルダウンして、地理的検索のために郡を選択するビューに移動します。したがって、検索ページは「郡の選択」ページにドリルダウンします。

NSMutableArray *counties2 番目のコントローラーをナビゲーション スタックにプッシュするときに、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 であることがわかります。

これはどのように起こりますか?クラスバンドルについては理解していますが(またはとにかく理解していると思います)、これは明示的に特定のタイプであり、ある時点でサブクラス化してすべてを殺す方法で失われています。なぜそれが起こるのか、私にはまったく理解できません。

4

2 に答える 2

4

私の推測では、配列を適切に割り当てていない可能性があります (たとえば、NSMutableArray *arr = [[NSArray alloc] init]、または変数に を割り当てNSArrayていNSMutableArrayます。配列を初期化するコードを投稿できますか?

于 2010-05-17T14:11:51.457 に答える
1

郡として設定したオブジェクトをどこで初期化しますか? 多分あなたは次のような間違いをしました:

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

この場合、コンパイル エラーは表示されませんが、そのように作成された配列を変更することはできません。

于 2010-05-17T14:14:53.517 に答える