プロパティから可変配列を編集しようとしていますが、直接機能させることができないようです。動作しているように見えるが、非常に非効率的であると思われる次のコードについて考えてみます。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];
}
// ...