swift - 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,删除 UITableView 中的行

标签 swift uitableview core-data

<分区>

执行以下代码时出现上述错误:

func tableView(_ tableView: UITableView,
                        commit editingStyle: UITableViewCellEditingStyle,
                        forRowAt indexPath: IndexPath) {
    let eventsOnArray = selectedRecipient?.events.allObjects  // crashes here

    guard let eventToRemove = eventsOnArray![indexPath.row] as? Event, editingStyle == .delete else {
                return
    }
        managedContext.delete(eventToRemove)
        do {
            try managedContext.save()
            getEvents()
            self.eventList.reloadData()
        } catch let error as NSError {
            print("Saving error: \(error), description: \(error.userInfo)")
        }

}

详细错误是:

2018-03-11 12:20:49.732482-0400 Card Tracker[1516:29197] -[Recipient events]: unrecognized selector sent to instance 0x600000283840
2018-03-11 12:20:49.746477-0400 Card Tracker[1516:29197] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Recipient events]: unrecognized selector sent to instance 0x600000283840'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010fe2f12b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010ef76f41 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010feb0024 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x000000010fdb1f78 ___forwarding___ + 1432
    4   CoreFoundation                      0x000000010fdb1958 _CF_forwarding_prep_0 + 120
    5   Card Tracker                        0x000000010e62b773 _T012Card_Tracker010ViewEventsC10ControllerC05tableC0ySo07UITableC0C_SC0gC16CellEditingStyleO6commit10Foundation9IndexPathV8forRowAttF + 195
    6   Card Tracker                        0x000000010e62c177 _T012Card_Tracker010ViewEventsC10ControllerC05tableC0ySo07UITableC0C_SC0gC16CellEditingStyleO6commit10Foundation9IndexPathV8forRowAttFTo + 119
    7   UIKit                               0x0000000110410a5f -[UITableView _animateDeletionOfRowAtIndexPath:] + 177
    8   UIKit                               0x0000000110419a59 __82-[UITableView _contextualActionForDeletingRowAtIndexPath:usingPresentationValues:]_block_invoke + 59
    9   UIKit                               0x0000000110953d67 -[UIContextualAction executeHandlerWithView:completionHandler:] + 174
    10  UIKit                               0x0000000110c41374 -[UISwipeOccurrence _performSwipeAction:inPullview:swipeInfo:] + 702
    11  UIKit                               0x0000000110c42bd1 -[UISwipeOccurrence swipeActionPullView:tappedAction:] + 112
    12  UIKit                               0x0000000110d25ed2 -[UISwipeActionPullView _tappedButton:] + 138
    13  UIKit                               0x00000001102ae972 -[UIApplication sendAction:to:from:forEvent:] + 83
    14  UIKit                               0x000000011042dc3c -[UIControl sendAction:to:forEvent:] + 67
    15  UIKit                               0x000000011042df59 -[UIControl _sendActionsForEvents:withEvent:] + 450
    16  UIKit                               0x000000011042ce86 -[UIControl touchesEnded:withEvent:] + 618
    17  UIKit                               0x000000011089ebad _UIGestureEnvironmentSortAndSendDelayedTouches + 5560
    18  UIKit                               0x0000000110898a4d _UIGestureEnvironmentUpdate + 1506
    19  UIKit                               0x000000011089841f -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 484
    20  UIKit                               0x00000001108974cb -[UIGestureEnvironment _updateGesturesForEvent:window:] + 288
    21  UIKit                               0x0000000110325f14 -[UIWindow sendEvent:] + 4102
    22  UIKit                               0x00000001102c9365 -[UIApplication sendEvent:] + 352
    23  UIKit                               0x000000012c2fe49d -[UIApplicationAccessibility sendEvent:] + 85
    24  UIKit                               0x0000000110c15a1d __dispatchPreprocessedEventFromEventQueue + 2809
    25  UIKit                               0x0000000110c18672 __handleEventQueueInternal + 5957
    26  CoreFoundation                      0x000000010fdd2101 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    27  CoreFoundation                      0x000000010fe71f71 __CFRunLoopDoSource0 + 81
    28  CoreFoundation                      0x000000010fdb6a19 __CFRunLoopDoSources0 + 185
    29  CoreFoundation                      0x000000010fdb5fff __CFRunLoopRun + 1279
    30  CoreFoundation                      0x000000010fdb5889 CFRunLoopRunSpecific + 409
    31  GraphicsServices                    0x00000001159789c6 GSEventRunModal + 62
    32  UIKit                               0x00000001102ad5d6 UIApplicationMain + 159
    33  Card Tracker                        0x000000010e606727 main + 55
    34  libdyld.dylib                       0x000000011436dd81 start + 1
    35  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

我正在尝试删除基于 Header-Detail 的实体中的详细信息行。一旦我离开 let eventsOnArray 行,调试器就会发生崩溃。我在该行放置了一个断点,代码一直运行到该点,然后在我使用“Step Over”时崩溃。

获取事件:

 func getEvents () {
        // Now load all Events for this Receipient
        let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Event")
        request.resultType = .dictionaryResultType

        do {
            events = try managedContext.fetch(request) as! [NSDictionary]
        } catch {
            print("Core Data  Fetch Failed:", error.localizedDescription)
        }
    }

核心数据定义:entity definition

最佳答案

很可能 selectedRecepient 不是 Recipient 类的实例(或者至少 ObjC 运行时认为如此)。 尝试检查它的类型:

print(type(of: selectedRecipient))

如果它打印 NSManagedObject 那么你应该确保 Recipient 实体在数据模型编辑器中将其类设置为 Recipient - 这告诉 Core将该实体的实例转换为相应类的数据。

关于swift - 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,删除 UITableView 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49223659/

相关文章:

Swift - 找不到 'init' 和 'round' 的重载

ios - 如何在运行时正确压缩 UIImages

arrays - iOS 如何找到浮点/整数值数组的值之间的最小差异

ios - 在 iOS 中,如何在两个单元格中的文本字段之间切换?

objective-c - NSManagedObjectContext,在哪里声明?

swift - 如何在 Swift 中创建嵌套的字典元素?

ios - 表格 View 单元格中的播放按钮显示错误的视频,因为它没有获得正确的单元格

ios - UITableView失去当前选择,而手指临时触摸另一行,仅在iPad上

ios - 将事件的 RecurrenceRule 保存到 CoreData

swift - CoreData 和 Codable 类编译器错误 : 'self.init' isn't called on all paths before returning from initializer