由于RLMException,Swift应用程序崩溃

时间:2019-04-25 18:29:05

标签: 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

2 个答案:

答案 0 :(得分:0)

这是索引越界异常:

  

“ RLMException”,原因:“索引2超出范围(必须小于   2)。'

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

  

如果让categoryForDeletion = self.categories?[indexPath.row]

如果行> = self.categories.count,则以上行将崩溃。我建议研究一下numberOfRowsForSection函数,以找出为什么行多于类别的原因。

答案 1 :(得分:0)

很难用很少的代码来说明发生了什么。

我建议用打印语句(尤其是array counts中的indexpath.rowcellForRow等)填充代码,以查看代码出问题的地方。

看起来您正在对领域数据库中不存在的行调用delete,因为它崩溃了。

这是我更新封包的方式

editActionsForRowAt