• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 致命异常 : NSRangeException - index 2 beyond bounds [0 .。 1]

[复制链接]
菜鸟教程小白 发表于 2022-12-13 16:25:09 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

在我在 swift 项目中使用的一个库中,一行导致应用程序崩溃。我试图理解并修复它,但没有运气。我知道这是由数组索引错误引起的。有人可以帮忙吗?

崩溃报告

Fatal Exception: NSRangeException
0  CoreFoundation                 0x180a42e38 __exceptionPreprocess
1  libobjc.A.dylib                0x1800a7f80 objc_exception_throw
2  CoreFoundation                 0x180922ebc -[__NSArrayM removeObjectAtIndex:]
3                          0x10000ac70 -[ChatSectionManager messageForIndexPath:] (ChatSectionManager.m:435)
4                          0x10001c194 -[Chat tableView:cellForRowAtIndexPath:] (Chat.m:596)
5  UIKit                          0x185ee2f40 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:]
6  UIKit                          0x185ee30a8 -[UITableView _createPreparedCellForGlobalRow:willDisplay:]

ChatSectionManager.m

 - (QBChatMessage *)messageForIndexPathNSIndexPath *)indexPath {

    if (indexPath.item == NSNotFound) {
        return nil;
    }

    QMChatSection *currentSection = self.chatSections[indexPath.section];
    //crashes here line 435
    return currentSection.messages[indexPath.item];
}

Chat.m

- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section {
    return [self.chatSectionManager messagesCountForSectionAtIndex:section];
}

- (NSInteger)numberOfSectionsInTableViewUITableView *)tableView {
    return self.chatSectionManager.chatSectionsCount;
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *) indexPath {

    QBChatMessage *messageItem = [self.chatSectionManager messageForIndexPath:indexPath];

... 方法包含 removeObjectAtIndex

- (void)deleteMessagesNSArray *)messages animatedBOOL)animated {

    dispatch_async(_serialQueue, ^{

        NSMutableArray *messagesIDs = [NSMutableArray array];
        NSMutableArray *itemsIndexPaths = [NSMutableArray array];
        NSMutableIndexSet *sectionsIndexSet = [NSMutableIndexSet indexSet];

        self.editableSections = self.chatSections.mutableCopy;

        for (QBChatMessage *message in messages) {
            NSIndexPath *indexPath = [self indexPathForMessage:message];
            if (indexPath == nil) continue;

            QMChatSection *chatSection = self.chatSections[indexPath.section];
            [chatSection.messages removeObjectAtIndex:indexPath.item];

            if (chatSection.isEmpty) {
                [sectionsIndexSet addIndex:indexPath.section];
                [self.editableSections removeObjectAtIndex:indexPath.section];

                // no need to remove elements whose section will be removed
                NSArray *items = [itemsIndexPaths copy];
                for (NSIndexPath *index in items) {
                    if (index.section == indexPath.section) {
                        [itemsIndexPaths removeObject:index];
                    }
                }
            } else {

                [itemsIndexPaths addObject:indexPath];
            }
        }

        dispatch_sync(dispatch_get_main_queue(), ^{

            self.chatSections = self.editableSections.copy;
            self.editableSections = nil;

            if ([self.delegate respondsToSelectorselector(chatSectionManager:didDeleteMessagesWithIDs:atIndexPaths:withSectionsIndexSet:animated]) {

                [self.delegate chatSectionManager:self didDeleteMessagesWithIDs:messagesIDs atIndexPaths:itemsIndexPaths withSectionsIndexSet:sectionsIndexSet animated:animated];
            }
        });
    });
}

注意:这个崩溃是随机发生的,我不知道为什么

织物崩溃报告 http://crashes.to/s/679e90f0c90



Best Answer-推荐答案


这绝对是deleteMessages的问题,看看你的线程16在哪里崩溃了:

QMChatSectionManager.m line 228 __48-[QMChatSectionManager deleteMessages:animated:]_block_invoke

检查第 228 行到底是哪一行以确定问题所在,但我会在这里冒险并猜测它是什么

[chatSection.messages removeObjectAtIndex:indexPath.item];

所以你要在线程 16 上删除 messageForIndexPath 在主线程上引用的数据结构。正如您所注意到的,这是对几乎不可能按需复制的竞争条件的公开邀请。

解决方法是不要在后台线程中修改 chatSection 的内容。看起来您已经有了正确的想法,在主线程上分配已完成的部分列表,

self.chatSections = self.editableSections.copy;

只需将该逻辑应用于部分列表中更改的每个项目,您的随机崩溃就会消失。

关于ios - 致命异常 : NSRangeException - index 2 beyond bounds [0 .。 1],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37150746/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap