gpt4 book ai didi

Objective-C NSMutableArray removeObjectAtIndex : crashes

转载 作者:行者123 更新时间:2023-12-01 17:59:24 25 4
gpt4 key购买 nike

我有一个带有表格 View 的 View Controller ,您可以在其中删除单元格。我有另一个处理称为书签的类,该类称为 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];

}

书签集合是一个可变数组,由具有名称和日期对象/键的字典填充。这些名称和日期是填充另一个 View Controller 中表格 View 单元格标题的内容。表格 View 中的单元格数量由 [[BookmarkHandler bookmarkCollection] count]; 决定。

在另一个 View Controller 中,您可以删除表格 View 单元格,所以我要做的是实现委托(delegate)方法:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {


if (editingStyle == UITableViewCellEditingStyleDelete) {

[BookmarkHandler deleteBookmark: indexPath];

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

所以,当我删除一个单元格时,我会从 BookmarkHandler 中删除一个书签。调用 deleteBookmark:并从 TableView 中删除该行。但有时这条线上会出现崩溃:
        [bookmarkCollection removeObjectAtIndex: indexPath.row];

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

有什么我做错了吗?感谢帮助...

最佳答案

问题在这里:

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

NSUserDefaults 不保存可变数组,它只是将它保存为 NSArray。
所以获取它的可变副本:
NSMutableArray *bookmarkCollection = [[[NSUserDefaults standardUserDefaults] objectForKey: @"bookmarks"] mutableCopy];

关于Objective-C NSMutableArray removeObjectAtIndex : crashes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12612504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com