0

我有一个我无法解决的问题。我正在使用 Core Data 框架创建一个 iOS 应用程序。在我得到 NSRangeException 之前,一切都很顺利。这个异常是我在检查应用程序数据是否需要更新的函数中得到的。

我的代码是:

- (BOOL) isTimetableUpdatedWithJSONData:(NSMutableDictionary*) data
{
    appDelegate = [[UIApplication sharedApplication] delegate];
    managedObjectContext = [appDelegate managedObjectContext];

    //Do database update check logic here
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:[NSEntityDescription entityForName:@"MobileTimetableDataHash"     inManagedObjectContext:managedObjectContext]];

    NSError *error = nil;
    NSArray *results = [managedObjectContext executeFetchRequest:request error:&error];

    if (error != nil)
    {
        [self printErrorAlert];
    }
    //THERE IS ERROR HERE
    NSManagedObject* changesGrabbed = [results objectAtIndex:0];
    NSString *changesFromDatabase = [changesGrabbed valueForKey:@"changes"];
    NSString *changesFromService = [data valueForKeyPath:@"changes"];

    if ([changesFromService isEqualToString:changesFromDatabase])
    {
        return YES;
    }
    else
    {
        [changesGrabbed setValue:changesFromService forKey:@"changes"];

        [managedObjectContext save:&error];

        if (error != nil)
        {
            [self printErrorAlert];
        }
        return NO;
    }

}

例外:

2014-06-12 21:36:12.034 MIF[437:60b] *** Terminating app due to uncaught exception     'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for     empty array'
*** First throw call stack:
(
0   CoreFoundation                      0x0000000101cb3495 __exceptionPreprocess +     165
1   libobjc.A.dylib                     0x0000000101a1299e objc_exception_throw + 43
2   CoreFoundation                      0x0000000101c6be3f -[__NSArrayI     objectAtIndex:] + 175
3   MIF                                 0x0000000100001942 -[MIFTimetableService     isTimetableUpdatedWithJSONData:] + 514
4   MIF                                 0x000000010000219d -[MIFAppDelegate     synchrinizeTimetableService] + 157
5   MIF                                 0x0000000100001dc2 -[MIFAppDelegate     application:didFinishLaunchingWithOptions:] + 114
6   UIKit                               0x00000001005ba3d9 -[UIApplication     _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 264
7   UIKit                               0x00000001005babe1 -[UIApplication     _callInitializationDelegatesForURL:payload:suspended:] + 1605
8   UIKit                               0x00000001005bea0c -[UIApplication     _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 660
9   UIKit                               0x00000001005cfd4c -[UIApplication     handleEvent:withNewEvent:] + 3189
10  UIKit                               0x00000001005d0216 -[UIApplication     sendEvent:] + 79
11  UIKit                               0x00000001005c0086 _UIApplicationHandleEvent     + 578
12  GraphicsServices                    0x0000000103ce671a _PurpleEventCallback +     762
13  GraphicsServices                    0x0000000103ce61e1 PurpleEventCallback + 35
14  CoreFoundation                      0x0000000101c35679     __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
15  CoreFoundation                      0x0000000101c3544e __CFRunLoopDoSource1 +    478
16  CoreFoundation                      0x0000000101c5e903 __CFRunLoopRun + 1939
17  CoreFoundation                      0x0000000101c5dd83 CFRunLoopRunSpecific +     467
18  UIKit                               0x00000001005be2e1 -[UIApplication _run] +     609
19  UIKit                               0x00000001005bfe33 UIApplicationMain + 1010
20  MIF                                 0x0000000100001d23 main + 115
21  libdyld.dylib                       0x000000010234b5fd start + 1
22  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

伙计们,有人可以帮帮我吗?我真的需要你的帮助。谢谢你。

4

3 回答 3

2

你的麻烦制造者似乎是

NSManagedObject* changesGrabbed = [结果 objectAtIndex:0];

如果results是一个空数组,调用objectAtIndex:0将导致 NSRangeException,因为索引 0 处没有对象。你最好调用 [results firstObject],如果 results 是空数组,它不会因错误而崩溃。在这种情况下,firstObject() 只返回 nil。

于 2014-06-12T19:06:41.277 回答
1

您正在尝试从空数组中检索对象。您应该首先检查数组是否包含任何对象。使用count属性:

if (results.count > 0) {
    NSManagedObject* changesGrabbed = [results objectAtIndex:0];
    //continue...
} else {
    //Array is empty, object 0 will be beyond bounds
}
于 2014-06-12T19:02:12.210 回答
1

就在之前

NSManagedObject* changesGrabbed = [results objectAtIndex:0];

根据返回的结果添加一个分支。类似于以下内容:

if (results && [results count] > 0) {
    NSManagedObject* changesGrabbed = [results objectAtIndex:0];
    ...
}

你的数组是空的。堆栈跟踪几乎为您说明了这一点-

原因:' * -[__NSArrayI objectAtIndex:]: 索引 0 超出
空数组的范围'

于 2014-06-12T19:03:29.933 回答