1

プロパティから可変配列を編集しようとしていますが、直接機能させることができないようです。動作しているように見えるが、非常に非効率的であると思われる次のコードについて考えてみます。1つのオブジェクトに変更を加えるためだけに、プロパティ配列全体をコピーする必要があります。「carNames」配列全体を変更できるのに、そのオブジェクトの1つを変更できないのはなぜですか。

あなたが提供できるかもしれないどんな光にも感謝します...

// CarTableViewController.h


// ...


@interface CarTableViewController : UITableViewController {
    NSMutableArray *carNames;
}

@property (nonatomic, retain) NSMutableArray *carNames;

-(IBAction)addCarButton:(id)sender;
// ...
// --------------------------------------

// -------CarTableViewController.m-------

// ...
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        // Could I somehow run the removeOjectAtIndex: 
        // directly on the carNames Property?  - this code works...

        NSMutableArray *mutablecarNames = [carNames mutableCopy];
        [mutablecarNames removeObjectAtIndex:indexPath.row];
        carNames = [mutablecarNames copy];
        [mutablecarNames release];

            // This code doesn't work... Why?
            // [carNames removeObjectAtIndex:indexPath.row];

    }

// ...
4

2 に答える 2

7

不変のNSArrayであるcarNamesの結果になるように設定しています。代わりに[mutableCarNames copy]必要です。mutableCopy

于 2009-11-18T16:49:43.437 に答える
1

なぜあなたはただ電話しないの[carNames removeObjectAtIndex:indexPath.row]ですか?

これは可変配列であるため、問題なく機能するはずです。

于 2009-11-18T20:53:41.527 に答える