【问题标题】:Terminating app due to uncaught exception 'NSInvalidArgumentException', on deleting Row in UITableView [duplicate]由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,删除 UITableView 中的行 [重复]
【发布时间】:2018-03-11 18:39:55
【问题描述】:

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

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)
        }
    }

核心数据定义:

【问题讨论】:

  • selectedRecipient 是如何定义和填充的?该错误暗示Recipient 类上没有events 属性。但大概代码编译了,所以编译器认为有一个event 属性。
  • selectedRecipient 是从先前的视图控制器传入的。 events 是 Recipient 和事件之间的关系。添加上面的定义
  • 这不是和your previous question一样的问题吗?没有属性events,关系被称为card - 为什么单数card 是一对多关系?
  • 您的模型显示关系名为card,而不是events。尝试将selectedRecipient?.events.allObjects 修改为selectedRecipient?.card.allObjects
  • 我的模型和它生成的代码之间的问题..已经清除了..并将卡更改为事件

标签: swift uitableview core-data


【解决方案1】:

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

print(type(of: selectedRecipient))

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

【讨论】:

  • Optional 是它打印的内容。我从前一个视图控制器中获得了 selectedRecepient (它是一个或多个附加事件的标题记录。)。它被定义为 var selectedRecipient: Recipient?在视图控制器类中。我在 getEvents() 函数中加载了与 selectedRecepient 相关的所有事件:我在上面添加的。
  • 发现模型和生成代码之间存在差异的问题。
猜你喜欢
  • 2015-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多