我该如何解决:由于未捕获的异常而终止应用程序 'NSUnknownKeyException'

How Do I Fix: Terminating app due to uncaught exception 'NSUnknownKeyException'

我读到其他人也有类似的未捕获异常,但大多数似乎是由缺少插座连接引起的。我不相信我的与 IBOutlets 有关,因为 VC 运行 在大多数情况下都很好,我的其他 VCs 也是如此。

我认为这与 Core Data 有关。这是错误,我认为它发生在我的 table 的 cellForRowAtIndexPath:

2016-01-05 15:18:14.947 Do List[1476:81278] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSManagedObject 0x7fbb89e7cc50> valueForUndefinedKey:]: the entity TomTask is not key value coding-compliant for the key "completedDate".'
*** First throw call stack:
(
0   CoreFoundation                      0x0000000102b5ee65 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x0000000104cfddeb objc_exception_throw + 48
2   CoreFoundation                      0x0000000102b5eaa9 -[NSException raise] + 9
3   CoreData                            0x0000000102757ec1 -[NSManagedObject valueForUndefinedKey:] + 289
4   Do List                             0x000000010235b9d6 _TFC7Do_List24AllocationViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 2630
5   Do List                             0x000000010235c24f _TToFC7Do_List24AllocationViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 79
6   UIKit                               0x0000000103933e43 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 766
7   UIKit                               0x0000000103933f7b -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
8   UIKit                               0x0000000103908a39 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2996
9   UIKit                               0x000000010393d01c -[UITableView _performWithCachedTraitCollection:] + 92
10  UIKit                               0x0000000103923edc -[UITableView layoutSubviews] + 224
11  UIKit                               0x00000001038914a3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
12  QuartzCore                          0x000000010369659a -[CALayer layoutSublayers] + 146
13  QuartzCore                          0x000000010368ae70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
14  QuartzCore                          0x000000010368acee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
15  QuartzCore                          0x000000010367f475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
16  QuartzCore                          0x00000001036acc0a _ZN2CA11Transaction6commitEv + 486
17  UIKit                               0x00000001037d4f7c _UIApplicationHandleEventQueue + 7329
18  CoreFoundation                      0x0000000102a8aa31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
19  CoreFoundation                      0x0000000102a8095c __CFRunLoopDoSources0 + 556
20  CoreFoundation                      0x0000000102a7fe13 __CFRunLoopRun + 867
21  CoreFoundation                      0x0000000102a7f828 CFRunLoopRunSpecific + 488
22  GraphicsServices                    0x0000000106bb7ad2 GSEventRunModal + 161
23  UIKit                               0x00000001037da610 UIApplicationMain + 171
24  Do List                             0x0000000102368fad main + 109
25  libdyld.dylib                       0x000000010a52392d start + 1
26  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

几点注意事项

有一组特定的 VCs如果 运行 以特定顺序,会导致此错误。

当我从 A -> B -> C 开始时发生错误,然后尝试放松到 A

如果您查看上面的异常堆栈,错误发生在 allocViewControllercellForRowAtIndexPath 中。 VCB ,所以它不应该被加载,只是被解开。

它引用了“CompletedDate”,这是我昨天添加的新内容,它启动了整个过程。 CompletedDate 是我刚开始在 view controller C.

中显示的现有 CoreData 属性

如果您查看错误的第一行,您似乎有一个名为 TomTask 的 class,您正尝试从中使用 "completedDate",但没有"completedDate" 在 TomTask 中。

看起来(从您的调用堆栈中的条目 #4)您正试图在该视图控制器的 tableView:cellForRowAtIndexPath: 方法中执行此操作。