Search code examples
ioscore-datansrangeexception

NSRangeException Xcode


I'm having a problem with my app and CoreData. I am using CoreData to save the user information and I have spent a long time searching how to use core data correctly. I have found and I learned how to use, but the problem have started when I deleted the app from the IOS simulator. When I did that the app freeze and the follow error appears in the console:

    2015-06-20 23:20:23.141 AppName[1767:218797] *** Terminating        
    app due to uncaught exception 'NSRangeException', reason: '*** -
    [__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'
    *** First throw call stack:
    (
0   CoreFoundation                      0x000000010a70ec65 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000010c279bb7 objc_exception_throw + 45
2   CoreFoundation                      0x000000010a60517e -[__NSArrayI objectAtIndex:] + 190
3   YoutubeBillionaire                  0x000000010a103df2 _TFC18YoutubeBillionaire14ViewController11viewDidLoadfS0_FT_T_ + 2562
4   YoutubeBillionaire                  0x000000010a1059f2 _TToFC18YoutubeBillionaire14ViewController11viewDidLoadfS0_FT_T_ + 34
5   UIKit                               0x000000010b0de210 -[UIViewController loadViewIfRequired] + 738
6   UIKit                               0x000000010b0de40e -[UIViewController view] + 27
7   UIKit                               0x000000010aff92c9 -[UIWindow addRootViewControllerViewIfPossible] + 58
8   UIKit                               0x000000010aff968f -[UIWindow _setHidden:forced:] + 247
9   UIKit                               0x000000010b005e21 -[UIWindow makeKeyAndVisible] + 42
10  UIKit                               0x000000010afa9457 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2732
11  UIKit                               0x000000010afac1de -[UIApplication _runWithMainScene:transitionContext:completion:] + 1349
12  UIKit                               0x000000010afab0d5 -[UIApplication workspaceDidEndTransaction:] + 179
13  FrontBoardServices                  0x000000010dd895e5 __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 21
14  CoreFoundation                      0x000000010a64241c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
15  CoreFoundation                      0x000000010a638165 __CFRunLoopDoBlocks + 341
16  CoreFoundation                      0x000000010a637f25 __CFRunLoopRun + 2389
17  CoreFoundation                      0x000000010a637366 CFRunLoopRunSpecific + 470
18  UIKit                               0x000000010afaab42 -[UIApplication _run] + 413
19  UIKit                               0x000000010afad900 UIApplicationMain + 1282
20  YoutubeBillionaire                  0x000000010a1192e7 main + 135
21  libdyld.dylib                       0x000000010c9d1145 start + 1
   )
    libc++abi.dylib: terminating with uncaught exception of type NSException
    (lldb) 

Thanks everyone.


Solution

  • The error is pretty self-explanatory; You're trying to access a value in an empty array. Your CoreData request must not be returning any results. Make sure you check for an empty array before trying to access elements in it. If you delete your app from your device/the simulator it deletes all the data in CoreData, thus any request would return an empty array.