Chapter6 - page.165: uncaught exception error after creating custom policy

Hello,

In book core data by tutorials v2.2 I’ve progressed to page 158 successfully, but I got stuck at complex mapping model. I’ve followed the steps exactly until the page 165 but I got this error message when I click run.

> 2016-04-28 02:27:46.468 UnCloudNotes[15810:1945619] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSTaggedPointerString substringToIndex:]: Index 80 out of bounds; string length 3'
> *** First throw call stack:
> (
> 	0   CoreFoundation                      0x000000010be6ed85 __exceptionPreprocess + 165
> 	1   libobjc.A.dylib                     0x000000010dc12deb objc_exception_throw + 48
> 	2   CoreFoundation                      0x000000010be6ecbd +[NSException raise:format:] + 205
> 	3   Foundation                          0x000000010c21a79f -[NSString substringToIndex:] + 118
> 	4   UnCloudNotes                        0x000000010b8d7a4b _TFC12UnCloudNotes48AttachmentToImageAttachmentMigrationPolicyV3toV443createDestinationInstancesForSourceInstancefzTCSo15NSManagedObject13entityMappingCSo15NSEntityMapping7managerCSo18NSMigrationManager_T_ + 1547
> 	5   UnCloudNotes                        0x000000010b8d7d17 _TToFC12UnCloudNotes48AttachmentToImageAttachmentMigrationPolicyV3toV443createDestinationInstancesForSourceInstancefzTCSo15NSManagedObject13entityMappingCSo15NSEntityMapping7managerCSo18NSMigrationManager_T_ + 119
> 	6   CoreData                            0x000000010ba92d6b -[NSMigrationManager(InternalMethods) _doFirstPassForMapping:error:] + 427
> 	7   CoreData                            0x000000010ba94379 -[NSMigrationManager(InternalMethods) _migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptions:error:] + 1897
> 	8   CoreData                            0x000000010ba91156 -[NSMigrationManager migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptions:error:] + 838
> 	9   CoreData                            0x000000010bae049c -[NSStoreMigrationPolicy(InternalMethods) migrateStoreAtURL:toURL:storeType:options:withManager:error:] + 284
> 	10  CoreData                            0x000000010badf718 -[NSStoreMigrationPolicy migrateStoreAtURL:withManager:metadata:options:error:] + 104
> 	11  CoreData                            0x000000010bae0fa5 -[NSStoreMigrationPolicy(InternalMethods) _gatherDataAndPerformMigration:] + 2565
> 	12  CoreData                            0x000000010ba99b10 __91-[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:]_block_invoke + 5184
> 	13  CoreData                            0x000000010baa64dd gutsOfBlockToNSPersistentStoreCoordinatorPerform + 189
> 	14  libdispatch.dylib                   0x000000010e6ab3eb _dispatch_client_callout + 8
> 	15  libdispatch.dylib                   0x000000010e68fef5 _dispatch_barrier_sync_f_invoke + 393
> 	16  CoreData                            0x000000010ba975d5 _perform + 197
> 	17  CoreData                            0x000000010b9a53e0 -[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:] + 400
> 	18  UnCloudNotes                        0x000000010b8d6c3c _TFFC12UnCloudNotes13CoreDataStackg11coordinatorCSo28NSPersistentStoreCoordinatorU_FT_S1_ + 604
> 	19  UnCloudNotes                        0x000000010b8d63ef _TFC12UnCloudNotes13CoreDataStackg11coordinatorCSo28NSPersistentStoreCoordinator + 239
> 	20  UnCloudNotes                        0x000000010b8d70d4 _TFFC12UnCloudNotes13CoreDataStackg7contextCSo22NSManagedObjectContextU_FT_S1_ + 68
> 	21  UnCloudNotes                        0x000000010b8d657f _TFC12UnCloudNotes13CoreDataStackg7contextCSo22NSManagedObjectContext + 239
> 	22  UnCloudNotes                        0x000000010b8cbab6 _TFFC12UnCloudNotes23NotesListViewControllerg5notesCSo26NSFetchedResultsControllerU_FT_S1_ + 422
> 	23  UnCloudNotes                        0x000000010b8c8f87 _TFC12UnCloudNotes23NotesListViewControllerg5notesCSo26NSFetchedResultsController + 247
> 	24  UnCloudNotes                        0x000000010b8c90f7 _TFC12UnCloudNotes23NotesListViewController14viewWillAppearfSbT_ + 119
> 	25  UnCloudNotes                        0x000000010b8c94a1 _TToFC12UnCloudNotes23NotesListViewController14viewWillAppearfSbT_ + 49
> 	26  UIKit                               0x000000010c8372bd -[UIViewController _setViewAppearState:isAnimating:] + 710
> 	27  UIKit                               0x000000010c837958 -[UIViewController __viewWillAppear:] + 149
> 	28  UIKit                               0x000000010c876c83 -[UINavigationController _startTransition:fromViewController:toViewController:] + 781
> 	29  UIKit                               0x000000010c877c4d -[UINavigationController _startDeferredTransitionIfNeeded:] + 890
> 	30  UIKit                               0x000000010c878d0b -[UINavigationController __viewWillLayoutSubviews] + 57
> 	31  UIKit                               0x000000010ca27503 -[UILayoutContainerView layoutSubviews] + 248
> 	32  UIKit                               0x000000010c751980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
> 	33  QuartzCore                          0x0000000111146c00 -[CALayer layoutSublayers] + 146
> 	34  QuartzCore                          0x000000011113b08e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
> 	35  QuartzCore                          0x000000011113af0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
> 	36  QuartzCore                          0x000000011112f3c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
> 	37  QuartzCore                          0x000000011115d086 _ZN2CA11Transaction6commitEv + 486
> 	38  QuartzCore                          0x000000011115d7f8 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
> 	39  CoreFoundation                      0x000000010bd93c37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
> 	40  CoreFoundation                      0x000000010bd93ba7 __CFRunLoopDoObservers + 391
> 	41  CoreFoundation                      0x000000010bd8911c CFRunLoopRunSpecific + 524
> 	42  UIKit                               0x000000010c691f21 -[UIApplication _run] + 402
> 	43  UIKit                               0x000000010c696f09 UIApplicationMain + 171
> 	44  UnCloudNotes                        0x000000010b8d4832 main + 114
> 	45  libdyld.dylib                       0x000000010e6df92d start + 1
> 	46  ???                                 0x0000000000000001 0x0 + 1
> )
> libc++abi.dylib: terminating with uncaught exception of type NSException
> (lldb) 

I can supply any further material regarding my progress if necessary.

Thank you in advance.

I really don’t know much about this debugging logs but as it was saying

‘*** -[NSTaggedPointerString substringToIndex:]: Index 80 out of bounds; string length 3’

I’ve searched for substringToIndex and found it on AttachmentToImageAttachmentMigrationPolicyV3toV4 class and changed it’s value from 80 to 3 then all my previous data appeared. Since I’ve created new notes with the new feature every time I made a progress in chapter 6 I was able to follow the improvement of feature set so far without an error. But why did I get this error at first place?

And the strangest thing is that after it run successfully with value 3 I’ve changed it back to 80 and it run successfully again!!

A good explanation on this is really appreciated.

I had this same problem too. After reading the documentation for substringToIndex(), I decided that I did not want to truncate the note body to 80 characters, and changed the code to this, so that the conversion would get all of the text:

newAttachment.setValue(body, forKey: “caption”)

In other words, I removed the substringToIndex() method call from “body”.

Perhaps when this tutorial was written this method used to copy the substring up to the index passed in or the end of the string if it was shorter in the past, even if the index was more than the actual string length, and now errors out if the index extends beyond the end of the string?

Hope that helps!