0

你好

我想通过 UITableView 从 NSMutableArray 中删除项目,但是应用程序崩溃了。崩溃是“ 0 objc_msgSend”。

这是我的代码伙计们:

- (void)viewDidLoad
{
    paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    path = [basePath stringByAppendingPathComponent:@"favoris.plist"];
    dict = [[NSArray arrayWithContentsOfFile:path] mutableCopy];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

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

    [dict removeObjectAtIndex:indexPath.row];

    [self.tableView reloadData];
}

谢谢

4

1 回答 1

0

EXEC_BAD_ACCESS 绝对意味着有一个僵尸,所以我认为你的内存管理有一些奇怪的地方,这可能是由于你如何实例化数组而发生的。在 Instruments 中运行 Zombies 工具绝对会为您提供更多信息,但可以尝试以下方法:

(另一个建议,你为什么将 NSArray 的变量命名为 dict?)。

试试这个代码:

- (void)viewDidLoad
{
    paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    path = [[basePath stringByAppendingPathComponent:@"favoris.plist"] retain];
    dict = [[NSMutableArray alloc] initWithContentsOfFile:path];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [dict removeObjectAtIndex:indexPath.row];
    [dict writeToFile:path atomically:YES]; 
    [self.tableView reloadData];
}

这个建议的基础是我怀疑调用copy自动释放的对象会产生意想不到的后果。

于 2012-11-10T16:17:38.787 回答