Swift 应用程序因 RLMException 而崩溃

标签 swift realm

我目前正在编写一个程序,这将是我的专业“计算机科学”的最后一个项目。我正在制作的应用程序是用 Swift 编写的。它基本上是一个应用程序,允许编写和分类用户计划每天、每月或每年执行的任务。我刚刚陷入了调试控制台。我无法弄清楚。

我问我的大多数同学是否可以帮助我尝试了解问题的根源。

问题可能来自于通过以下代码编写的 searchBar 功能:

extension CategoryViewController: SwipeTableViewCellDelegate {

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
        guard orientation == .right else { return nil }

        let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
            // handle action by updating model with deletion

            if let categoryForDeletion = self.categories?[indexPath.row] {
                do {
                    try self.realm.write {
                        self.realm.delete(categoryForDeletion)
                    }
                } catch {
                    print("Error deleting the category, \(error)")
                }
            }
        }

        // customize the action appearance
        deleteAction.image = UIImage(named: "delete-icon")

        return [deleteAction]
    }

    func collectionView(_ collectionView: UICollectionView, editActionsOptionsForItemAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeOptions {
        var options = SwipeOptions()
        options.expansionStyle = .destructive
        return options
    }
}

我遇到了问题:

'RLMException', reason: 'Index 2 is out of bounds (must be less than 2).'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010519e1bb __exceptionPreprocess + 331
    1   libobjc.A.dylib                     0x00000001038e6735 objc_exception_throw + 48
    2   Realm                               0x00000001028b4d3e _Z20RLMThrowResultsErrorP8NSString + 670
    3   Realm                               0x00000001028b5fa6 _ZL25translateRLMResultsErrorsIZ28-[RLMResults objectAtIndex:]E3$_6EDaOT_P8NSString + 118
    4   Realm                               0x00000001028b5ece -[RLMResults objectAtIndex:] + 110
    5   RealmSwift                          0x0000000101ef2e3c $S10RealmSwift7ResultsCyxSicig + 220
    6   Todoey2                             0x0000000101aea48f $S7Todoey222CategoryViewControllerC05tableC0_19editActionsForRowAt3forSay12SwipeCellKit0L6ActionCGSgSo07UITableC0C_10Foundation9IndexPathVAG0lG11OrientationOtFyAI_APtcfU_ + 303
    7   Todoey2                             0x0000000101aea8e2 $S7Todoey222CategoryViewControllerC05tableC0_19editActionsForRowAt3forSay12SwipeCellKit0L6ActionCGSgSo07UITableC0C_10Foundation9IndexPathVAG0lG11OrientationOtFyAI_APtcfU_TA + 18
    8   SwipeCellKit                        0x000000010224c3e8 $S12SwipeCellKit0A10ControllerC7perform6action4hideyAA0A6ActionC_SbtF + 776
    9   SwipeCellKit                        0x0000000102245d99 $S12SwipeCellKit0A10ControllerC7perform6actionyAA0A6ActionC_tF + 1241
    10  SwipeCellKit                        0x000000010224b734 $S12SwipeCellKit0A10ControllerC16swipeActionsView_9didSelectyAA0afG0C_AA0A6ActionCtF + 52
    11  SwipeCellKit                        0x000000010224e199 $S12SwipeCellKit0A10ControllerCAA0A19ActionsViewDelegateA2aDP05swipeeF0_9didSelectyAA0aeF0C_AA0A6ActionCtFTW + 9
    12  SwipeCellKit                        0x000000010222c40e $S12SwipeCellKit0A11ActionsViewC12actionTapped6buttonyAA0A12ActionButtonC_tF + 590
    13  SwipeCellKit                        0x000000010222c47c $S12SwipeCellKit0A11ActionsViewC12actionTapped6buttonyAA0A12ActionButtonC_tFTo + 60
    14  UIKitCore                           0x000000010d233ecb -[UIApplication sendAction:to:from:forEvent:] + 83
    15  UIKitCore                           0x000000010cc6f0bd -[UIControl sendAction:to:forEvent:] + 67
    16  UIKitCore                           0x000000010cc6f3da -[UIControl _sendActionsForEvents:withEvent:] + 450
    17  UIKitCore                           0x000000010cc6e31e -[UIControl touchesEnded:withEvent:] + 583
    18  UIKitCore                           0x000000010ce07018 _UIGestureEnvironmentSortAndSendDelayedTouches + 5387
    19  UIKitCore                           0x000000010ce00fd1 _UIGestureEnvironmentUpdate + 1506
    20  UIKitCore                           0x000000010ce009ad -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 478
    21  UIKitCore                           0x000000010ce0071d -[UIGestureEnvironment _updateForEvent:window:] + 200
    22  UIKitCore                           0x000000010d27078a -[UIWindow sendEvent:] + 4058
    23  UIKitCore                           0x000000010d24e394 -[UIApplication sendEvent:] + 352
    24  UIKitCore                           0x000000010d3235a9 __dispatchPreprocessedEventFromEventQueue + 3054
    25  UIKitCore                           0x000000010d3261cb __handleEventQueueInternal + 5948
    26  CoreFoundation                      0x0000000105103721 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    27  CoreFoundation                      0x0000000105102f93 __CFRunLoopDoSources0 + 243
    28  CoreFoundation                      0x00000001050fd63f __CFRunLoopRun + 1263
    29  CoreFoundation                      0x00000001050fce11 CFRunLoopRunSpecific + 625
    30  GraphicsServices                    0x000000010a6731dd GSEventRunModal + 62
    31  UIKitCore                           0x000000010d23281d UIApplicationMain + 140
    32  Todoey2                             0x0000000101aef817 main + 71
    33  libdyld.dylib                       0x0000000104ce9575 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

最佳答案

这是索引越界异常:

'RLMException', reason: 'Index 2 is out of bounds (must be less than 2).'

当您对数组的下标索引大于或等于数组长度(等于,因为数组索引从 0 而不是 1 开始)时,就会发生这种情况。

if let categoryForDeletion = self.categories?[indexPath.row]

如果 row >= self.categories.count,上面的行将会崩溃。我建议查看 numberOfRowsForSection 函数,以找出行数多于类别数的原因。

关于Swift 应用程序因 RLMException 而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55855569/

相关文章:

swift - 图例是否限制标题的数量?

swift - 当连接 airpods 时,routeChangeNotification 在音频切换到扬声器后检测到 newDeviceAvailable

ios - Realm 更新 Swift 无法识别的选择器发送到实例

ios - 尝试删除对象类型的子对象时 Realm 崩溃

ios - Realm 不支持 armv7s

ios - 设计导航项右栏按钮,在传递数据时使用 segue 到另一个 VC

ios - 如何对齐 imageView 内的图像

ios - UICollectionViewController 的子类中多次使用 collectionView 之间的名称冲突

swift - 防止在 Realm 通知 block 中使用弱/无主 self 保留循环

javascript - 无法在已解决的 promise 中返回处于写入状态的打开的 Realm 实例