Search code examples
iosswiftcore-datauncaught-exception

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


I've read that other people have had similar uncaught exceptions, however most seem to be caused by a missing outlet connection. I don't believe mine is related to IBOutlets because the VC runs fine in most scenarios and so do my other VCs.

I think it has to do with Core Data. Here's the error, which I believe occurs in my table's 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

A Few Notes

There's a particular set of VCs that, if run in a particular order, cause this error.

The error occurs when I go from A -> B -> C, then try to unwind to A.

If you look at the exception stack above, the error is happening in the cellForRowAtIndexPath of the allocViewController. That VC is B ,so it shouldn't be loading, just being unwound through.

It references "CompletedDate" which is the new thing I added yesterday that kick this whole thing off. CompletedDate was an existing CoreData attribute which I just started showing in view controller C.


Solution

  • If you look at the first line of the error, it looks like you have a class called TomTask which you're trying to use the "completedDate" from, but there isn't a "completedDate" in TomTask.

    It appears (from entry #4 in your call stack) you're trying to do this in your tableView:cellForRowAtIndexPath: method of that view controller.