How to check for nil on a Realm Results array value

2020-02-15 ios swift xcode realm

I'm trying to figure out why I cannot check for nil on the following function call:

var allExercises : Results<Exercises>?
var exerciseIndex : Int = 0

        @objc func goToNextExercise(){

            if allExercises?[exerciseIndex] != nil {
                print("Does not equal nil")
                exerciseIndex += 1
            } else {
                print("Does equal nil")
            }

        }

When allExercises?[exerciseIndex] does equal nil, the application crashes and I get an "indexPath out of Range" error:

*** Terminating app due to uncaught exception 'RLMException', reason: 'Index 3 is out of bounds (must be less than 3).'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000104a7127e __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x00000001048deb20 objc_exception_throw + 48
    2   SectionRowsTutorial                 0x00000001025d7aae _Z20RLMThrowResultsErrorP8NSString + 670
    3   SectionRowsTutorial                 0x00000001025d8cc6 _ZL25translateRLMResultsErrorsIZ28-[RLMResults objectAtIndex:]E3$_6EDaOT_P8NSString + 118
    4   SectionRowsTutorial                 0x00000001025d8bf4 -[RLMResults objectAtIndex:] + 100
    5   SectionRowsTutorial                 0x000000010278c9dc $s10RealmSwift7ResultsVyxSicig + 236
    6   SectionRowsTutorial                 0x00000001022dc576 $s19SectionRowsTutorial19ThirdViewControllerC16goToNextExerciseyyF + 246
    7   SectionRowsTutorial                 0x00000001022dc87b $s19SectionRowsTutorial19ThirdViewControllerC16goToNextExerciseyyFTo + 43
    8   UIKitCore                           0x0000000108571fff -[UIApplication sendAction:to:from:forEvent:] + 83
    9   UIKitCore                           0x0000000107f4a00e -[UIControl sendAction:to:forEvent:] + 223
    10  UIKitCore                           0x0000000107f4a358 -[UIControl _sendActionsForEvents:withEvent:] + 398
    11  UIKitCore                           0x0000000107f492b7 -[UIControl touchesEnded:withEvent:] + 481
    12  UIKitCore                           0x00000001085acbbf -[UIWindow _sendTouchesForEvent:] + 2604
    13  UIKitCore                           0x00000001085ae4c6 -[UIWindow sendEvent:] + 4596
    14  UIKitCore                           0x000000010858953b -[UIApplication sendEvent:] + 356
    15  UIKitCore                           0x000000010860a71a __dispatchPreprocessedEventFromEventQueue + 6847
    16  UIKitCore                           0x000000010860d1e0 __handleEventQueueInternal + 5980
    17  CoreFoundation                      0x00000001049d4471 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    18  CoreFoundation                      0x00000001049d439c __CFRunLoopDoSource0 + 76
    19  CoreFoundation                      0x00000001049d3b74 __CFRunLoopDoSources0 + 180
    20  CoreFoundation                      0x00000001049ce87f __CFRunLoopRun + 1263
    21  CoreFoundation                      0x00000001049ce066 CFRunLoopRunSpecific + 438
    22  GraphicsServices                    0x000000010e756bb0 GSEventRunModal + 65
    23  UIKitCore                           0x0000000108570d4d UIApplicationMain + 1621
    24  SectionRowsTutorial                 0x00000001022e759b main + 75
    25  libdyld.dylib                       0x0000000105ba4c25 start + 1
    26  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Answers

You are checking the value of allExercises, if you pass a greater index than the array size, it's crashing. for check, you should first check if exerciseIndex is less than allExercises count - 1 and then check data


if  exerciseIndex <= allExercises.count - 1

Related