如何在Objective C中编辑可变属性?

Mic*_*ael 1 properties objective-c uitableview nsmutablearray

我试图从属性编辑一个可变数组,似乎无法直接使其工作.考虑以下似乎有用的代码,但似乎也非常低效; 我必须复制整个属性数组才能在一个对象中进行更改.为什么我可以更改整个"carNames"数组,但不能对其中一个对象进行更改?

感谢您提供的任何光线......

// 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];

    }

// ...
Run Code Online (Sandbox Code Playgroud)

Chu*_*uck 7

您将设置carNames为结果[mutableCarNames copy],这是一个不可变的NSArray.你想要的mutableCopy.