0

こんにちは私はiTunesUCS193Pの9番目のレッスンでテーブルビューについて勉強しています。コンパイラはこのエラーを報告しますNSInternalInconsistencyException'、理由:'-[__ NSCFArray removeObjectAtIndex:]:変更メソッドが不変オブジェクトに送信されました'

私のシミュレーターはiPad6.1です

ので、私は持っています

GraphViewController.mというクラス

#define FAVORITE_KEY @"GraphViewController.Favorite"
- (IBAction)addFavorite:(id)sender
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray *favorites = [[defaults objectForKey:FAVORITE_KEY] mutableCopy];
    if (!favorites) favorites = [NSMutableArray array];
    [favorites addObject:self.program];
   // NSLog(@"contenuto favorites %@",favorites);
    [defaults setObject:favorites forKey:FAVORITE_KEY];
    [defaults synchronize];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"GraphTableView"]) {
        NSArray *program = [[NSUserDefaults standardUserDefaults]objectForKey:FAVORITE_KEY];
        [segue.destinationViewController setPrograms:program];
    }
}

(setProgramsは、CalculatorTVC.mと呼ばれるテーブルビューコントローラーで送信するデータがあるセッターです)

CalculatorTVC.mと呼ばれるクラス

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.programs count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cellTable";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    id program = [self.programs objectAtIndex:indexPath.row];

    cell.textLabel.text = [@"y = " stringByAppendingString:[CalculatorBrain descriptionProgram:program]];
    NSLog(@"contenuto di program %@",program);  

    return cell;
}

(プログラムは、GraphViewController.mからのデータを配置するパブリックプロパティです)

私のストーリーボードには分割ビューがあります...MasterViewControllerには、ポップオーバースタイル識別子のテーブルビューコントローラー(CalculatorTVC.m)で配線されたバーボタンアイテム付きのツールバーがあります。

エラーは *キャッチされない例外によるアプリの終了'NSInternalInconsistencyException'、理由:'-[__ NSCFArray removeObjectAtIndex:]:不変オブジェクトに送信された変更メソッド' *最初のスロー呼び出しスタック:(0x1ca1012 0x10dee7e 0x1ca0deb 0x1d21c4f 0x1d21911 0x4a 0x10f26b0 0x229dfc0 0x229233c 0x22a0238 0x6b6e3 0x4f1476 0x870989a 0x4f2555 0x489ef9 0x46ab99 0x46ac14 0x10f2705 0x262c0 0x262a64 0x10f2705 0x262c0 0x26258 0xe7021 0xe757f 0xe66e8 0x55cef 0x55f02 0x33d4a 0x25698 0x1bfcdf9 0x1bfcad0 0x1c16bf5 0x1c16962 0x1c47bb6 0x1c46f44 0x1c46e1b 0x1bfb7e3 0x1bfb668 0x22ffc 0x24bd 0x23e5)のlibc ++ abi.dylib:(例外をスロー呼ばTERMINATE lldb)

私を助けてください

ご理解のほどよろしくお願いいたします。


わかりました、クラッシュする場所を見つけました。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cellTable";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    id program = [self.programs objectAtIndex:indexPath.row];

    cell.textLabel.text = [@"y = " stringByAppendingString:[CalculatorBrain descriptionProgram:program]];
    NSLog(@"contenuto di program %@",program);  

    return cell;
}

ラインのクラッシュ

cell.textLabel.text = [@ "y =" stringByAppendingString:[CalculatorBrain descriptionProgram:program]];

この行の前にNSLOGを配置すると、出力NSLOG結果に表示されます...しかし、この後にNSLOGを配置すると、出力に何も表示されません。

4

1 に答える 1

0
 NSArray *program = [[NSUserDefaults standardUserDefaults]objectForKey:FAVORITE_KEY];
[segue.destinationViewController setPrograms:program];

ここでは、不変の配列を宛先ビュー コントローラーに渡しています。変更可能な配列が必要で、それを変更しようとすると、そのクラッシュが発生します。ここでも mutableCopy が必要です。

プロパティ変更可能な配列である必要がある場合は、コンパイラの警告が表示されます。これらを無視しないでください!

ちなみに、追加ではなく削除でクラッシュしているため、質問に正しいコードが含まれていません。例外ブレークポイントを有効にして、クラッシュしている行を見つけます。

于 2013-03-08T18:11:01.987 に答える