gpt4 book ai didi

iOS 核心数据 NSRangeException

转载 作者:行者123 更新时间:2023-11-28 19:01:57 26 4
gpt4 key购买 nike

我有一个我无法解决的问题。我正在使用 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;
}

}

异常(exception):

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)

伙计们,有谁能帮帮我吗?我真的需要你的帮助。谢谢。

最佳答案

你的麻烦制造者似乎是

NSManagedObject* changesGrabbed = [results objectAtIndex:0];

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

关于iOS 核心数据 NSRangeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24191900/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com