如何检查领域结果数组值是否为零

问题描述 投票:0回答:1

我试图弄清楚为什么我不能在以下函数调用中检查nil:

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")
            }

        }

[allExercises?[exerciseIndex]等于nil时,应用程序崩溃,并且出现“ indexPath超出范围”错误:

*** 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
ios swift xcode realm
1个回答
1
投票

您正在检查allExercises的值,如果传递的索引大于数组的大小,则会崩溃。要进行检查,首先应检查exerciseIndex是否小于allExercises计数-1,然后检查数据

© www.soinside.com 2019 - 2024. All rights reserved.