Objective-C NSMutableArray removeObjectAtIndex:崩溃

时间:2012-09-27 00:47:25

标签: objective-c ios xcode uitableview nsmutablearray

我有一个带有表视图的视图控制器,您可以在其中删除单元格。我有另一个处理书签的东西,该类叫做BookmarkHandler。有一些方法可以上传书签,获取整个书签数组并删除书签。这节课如下:

+ (NSMutableArray *)bookmarkCollection {

NSMutableArray *bookmarkCollection = [[NSUserDefaults standardUserDefaults] objectForKey: @"bookmarks"];

if (!bookmarkCollection) {

    bookmarkCollection = [[NSMutableArray alloc] init];
}

return bookmarkCollection;
}

+ (void)deleteBookmark: (NSIndexPath *)indexPath {

    NSMutableArray *bookmarkCollection = [[NSUserDefaults standardUserDefaults] objectForKey: @"bookmarks"];

    [bookmarkCollection removeObjectAtIndex: indexPath.row];

    [[NSUserDefaults standardUserDefaults] setObject:bookmarkCollection forKey: @"bookmarks"];

    [[NSUserDefaults standardUserDefaults] synchronize];

}
+ (void)uploadBookmark:(NSDictionary *)singleBookmark {

    NSMutableArray *bookmarkCollection = [[NSUserDefaults standardUserDefaults] objectForKey: @"bookmarks"];


if (!bookmarkCollection) {

    bookmarkCollection = [[NSMutableArray alloc] init];
} 

NSMutableDictionary *bookmark1 = [[NSMutableDictionary alloc] initWithDictionary: singleBookmark];

NSMutableDictionary *bookmark2 = [[NSMutableDictionary alloc] initWithDictionary: singleBookmark];


NSNumber *number1 = [[NSNumber alloc] initWithInt: 1];
NSNumber *number2 = [[NSNumber alloc] initWithInt: 2];

[bookmark1 setObject:number1 forKey: @"bookmarkTag"];
[bookmark2 setObject:number2 forKey: @"bookmarkTag"];

[bookmarkCollection addObject: bookmark1];
[bookmarkCollection addObject: bookmark2];

[[NSUserDefaults standardUserDefaults] setObject:bookmarkCollection forKey: @"bookmarks"];

[[NSUserDefaults standardUserDefaults] synchronize];

}

作为可变数组的书签集合由具有名称和日期对象/键的字典填充。这些名称和日期是在另一个视图控制器中填充表格视图单元格标题的内容。表格视图中的单元格数由[[BookmarkHandler bookmarkCollection] count];

确定

在另一个视图控制器中,你可以删除表视图单元格,所以我要做的是实现委托方法:

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


if (editingStyle == UITableViewCellEditingStyleDelete) {

    [BookmarkHandler deleteBookmark: indexPath]; 

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}

因此,当我删除单元格时,我通过调用BookmarkHandlerdeleteBookmark:删除书签并从表格视图中删除该行。但有时候这条线路出现了崩溃:

        [bookmarkCollection removeObjectAtIndex: indexPath.row];

但是没有崩溃日志,我添加了一个All Exceptions断点。

我做错了吗?谢谢你的帮助...

2 个答案:

答案 0 :(得分:4)

问题在于:

NSMutableArray *bookmarkCollection = [[NSUserDefaults standardUserDefaults] objectForKey: @"bookmarks"];

NSUserDefaults不保存可变数组,只是将其保存为NSArray 所以拿它的可变副本:

NSMutableArray *bookmarkCollection = [[[NSUserDefaults standardUserDefaults] objectForKey: @"bookmarks"] mutableCopy];

答案 1 :(得分:1)

您应该检查indexPath.row以确保它不受限制。在此之后,你可以找到原因。

if (indexPath.row>=0 && indexPath.row<bookmarkCollection.count) {
    [bookmarkCollection removeObjectAtIndex: indexPath.row];
} else {
    NSLog(@"indexPath.row is out of boundry of bookmarkCellection size: %d", bookmarkCollection.count);
}