我正在尝试从属性编辑可变数组,但似乎无法直接使其工作。考虑以下似乎可以工作但效率也很低的代码;我必须复制整个属性数组才能对一个对象进行更改。为什么我可以更改整个“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];
}
// ...