在 UITableViewCellEditingStyleDelete 中使用 removeObjectAtIndex 时无法识别的选择器发送到实例错误

Unrecognized selector sent to instance error when using removeObjectAtIndex in UITableViewCellEditingStyleDelete

我是 ios 应用程序开发的新手,我在尝试从单元格中删除一行时遇到了这个问题:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString removeObjectAtIndex:]: unrecognized selector sent to instance 0x7fef12743830'

这是我的代码:

头文件:

#import <UIKit/UIKit.h>
#import "ViewController.h"

@interface TableViewController : UITableViewController <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSArray *titles;
@property (nonatomic,strong) NSDictionary *animeNames;
@end

viewDidLoad方法中我有这段代码设置self.titles的值并且我从plist文件

获取行数据
NSURL *url = [[NSBundle mainBundle] URLForResource:@"animes" withExtension:@"plist"];
    self.animeNames = [NSDictionary dictionaryWithContentsOfURL:url];
    self.titles = self.animeNames.allKeys;

实现文件

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        // Delete the row from the data source

        [tableView beginUpdates];

        NSMutableArray *current = [self.titles objectAtIndex:indexPath.row];

        [current removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        [tableView reloadData];
        [tableView endUpdates];

    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }

}

您的 titles 数组似乎包含 NSString 个对象。所以你不能这样称呼它:

NSMutableArray *current = [self.titles objectAtIndex:indexPath.row];
[current removeObjectAtIndex:indexPath.row];

还有一个NSArray,它是不可变的,所以你应该使用NSMutableArray代替(当你声明它为NSMutableArray时,你还应该确保分配数组也是一个NSMutableArray)。或者,您可以使用以下方法修复它:

NSMutableArray *current = [self.titles mutableCopy];
[current removeObjectAtIndex:indexPath.row];
self.titles = [current copy];