出现异常“索引 0 超出空数组范围”?

发布于 2024-11-19 11:35:52 字数 4254 浏览 1 评论 0原文

我通过日期选择器视图从用户那里获取开始日期和结束日期。当用户快速滚动选择器视图时,它会崩溃并给我这个异常。那么解决办法是什么?

[

NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x013fabe9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x0154f5c2 objc_exception_throw + 47
    2   CoreFoundation                      0x013f06e5 -[__NSArrayM objectAtIndex:] + 261
    3   iFeel                               0x0001e1b7 -[S7GraphView drawRect:] + 5714
    4   UIKit                               0x0047d6eb -[UIView(CALayerDelegate) drawLayer:inContext:] + 426
    5   QuartzCore                          0x01f6f9e9 -[CALayer drawInContext:] + 143
    6   QuartzCore                          0x01f6f5ef _ZL16backing_callbackP9CGContextPv + 85
    7   QuartzCore                          0x01f6edea CABackingStoreUpdate + 2246
    8   QuartzCore                          0x01f6e134 -[CALayer _display] + 1085
    9   QuartzCore                          0x01f6dbe4 CALayerDisplayIfNeeded + 231
    10  QuartzCore                          0x01f6038b _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 325
    11  QuartzCore                          0x01f600d0 _ZN2CA11Transaction6commitEv + 292
    12  QuartzCore                          0x01f907d5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
    13  CoreFoundation                      0x013dbfbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
    14  CoreFoundation                      0x013710e7 __CFRunLoopDoObservers + 295
    15  CoreFoundation                      0x01339bd7 __CFRunLoopRun + 1575
    16  CoreFoundation                      0x01339240 CFRunLoopRunSpecific + 208
    17  CoreFoundation                      0x01339161 CFRunLoopRunInMode + 97
    18  GraphicsServices                    0x019de268 GSEventRunModal + 217
    19  GraphicsServices                    0x019de32d GSEventRun + 115
    20  UIKit                               0x0045442e UIApplicationMain + 1160
    21  iFeel                               0x00002990 main + 102
    22  iFeel                               0x00002921 start + 53
)
terminate called after throwing an instance of 'NSException'

@deepak 和 @Grady Player 正在谈论的方法是这样的:

-(void)getGraphicalData{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    //[NSThread sleepForTimeInterval:1];

    self.friendBL = [[FriendBL alloc] init];
    self.result = [self.friendBL  getGraphicalData1:txtStartTime.text andTodate:txtEndTime.text];
    if ([self.result isKindOfClass:[NSString class]]) {
        UIAlertView * friendListAlertView = [[UIAlertView alloc] initWithTitle:@"Friend List" message:(NSString*) self.result delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [friendListAlertView show];
        [friendListAlertView release];
    }
    else {
        self.reportList = (NSMutableArray*) self.result;
        NSLog(@"%d",[reportList count]);
    }
    [pool release];
}

选择日期后,我单击按钮说完成,这是代码,

-(IBAction)doneBtn{

    [dateView setHidden:YES];
    NSLog(@"%@",[dateTimePicker date]);
    NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateStyle:NSDateFormatterBehaviorDefault];

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MM/dd/yyyy hh:mm:ss"];
    dateFormat.locale = locale;
    if (tag) {
        txtEndTime.text=[dateFormat stringFromDate:[dateTimePicker date]];
        if (![self compareDates]) {
            txtStartTime.text=@"";
            txtEndTime.text=@"";
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Date is not in valid" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }

    }
    else {
        txtStartTime.text=[dateFormat stringFromDate:[dateTimePicker date]];
    }
    NSLog(@"%@",[dateFormat stringFromDate:[dateTimePicker date]]);



}

但如果我慢慢选择日期,它不会崩溃

i am getting start date and end date from user via date pickerview. when user scroll the pickerview in speed then it crashes there and gives me this exception. so whats the solution?

[

NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x013fabe9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x0154f5c2 objc_exception_throw + 47
    2   CoreFoundation                      0x013f06e5 -[__NSArrayM objectAtIndex:] + 261
    3   iFeel                               0x0001e1b7 -[S7GraphView drawRect:] + 5714
    4   UIKit                               0x0047d6eb -[UIView(CALayerDelegate) drawLayer:inContext:] + 426
    5   QuartzCore                          0x01f6f9e9 -[CALayer drawInContext:] + 143
    6   QuartzCore                          0x01f6f5ef _ZL16backing_callbackP9CGContextPv + 85
    7   QuartzCore                          0x01f6edea CABackingStoreUpdate + 2246
    8   QuartzCore                          0x01f6e134 -[CALayer _display] + 1085
    9   QuartzCore                          0x01f6dbe4 CALayerDisplayIfNeeded + 231
    10  QuartzCore                          0x01f6038b _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 325
    11  QuartzCore                          0x01f600d0 _ZN2CA11Transaction6commitEv + 292
    12  QuartzCore                          0x01f907d5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
    13  CoreFoundation                      0x013dbfbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
    14  CoreFoundation                      0x013710e7 __CFRunLoopDoObservers + 295
    15  CoreFoundation                      0x01339bd7 __CFRunLoopRun + 1575
    16  CoreFoundation                      0x01339240 CFRunLoopRunSpecific + 208
    17  CoreFoundation                      0x01339161 CFRunLoopRunInMode + 97
    18  GraphicsServices                    0x019de268 GSEventRunModal + 217
    19  GraphicsServices                    0x019de32d GSEventRun + 115
    20  UIKit                               0x0045442e UIApplicationMain + 1160
    21  iFeel                               0x00002990 main + 102
    22  iFeel                               0x00002921 start + 53
)
terminate called after throwing an instance of 'NSException'

the method @deepak and @ Grady Player are talking about is this:

-(void)getGraphicalData{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    //[NSThread sleepForTimeInterval:1];

    self.friendBL = [[FriendBL alloc] init];
    self.result = [self.friendBL  getGraphicalData1:txtStartTime.text andTodate:txtEndTime.text];
    if ([self.result isKindOfClass:[NSString class]]) {
        UIAlertView * friendListAlertView = [[UIAlertView alloc] initWithTitle:@"Friend List" message:(NSString*) self.result delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [friendListAlertView show];
        [friendListAlertView release];
    }
    else {
        self.reportList = (NSMutableArray*) self.result;
        NSLog(@"%d",[reportList count]);
    }
    [pool release];
}

after selecting date i click on button to say done and this is the code

-(IBAction)doneBtn{

    [dateView setHidden:YES];
    NSLog(@"%@",[dateTimePicker date]);
    NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateStyle:NSDateFormatterBehaviorDefault];

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MM/dd/yyyy hh:mm:ss"];
    dateFormat.locale = locale;
    if (tag) {
        txtEndTime.text=[dateFormat stringFromDate:[dateTimePicker date]];
        if (![self compareDates]) {
            txtStartTime.text=@"";
            txtEndTime.text=@"";
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Date is not in valid" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }

    }
    else {
        txtStartTime.text=[dateFormat stringFromDate:[dateTimePicker date]];
    }
    NSLog(@"%@",[dateFormat stringFromDate:[dateTimePicker date]]);



}

but if i pick a the date slowly it do not crashes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

杀手六號 2024-11-26 11:35:52

由于您不是通过线程调用该方法,因此不必使用自动释放池。你可以试试下面的代码吗?

-(void)getGraphicalData{
    self.friendBL = [[[FriendBL alloc] init] autorelease];
    self.result = [self.friendBL  getGraphicalData1:txtStartTime.text andTodate:txtEndTime.text];
    if ([self.result isKindOfClass:[NSString class]]) {
        UIAlertView * friendListAlertView = [[UIAlertView alloc] initWithTitle:@"Friend List" message:(NSString*) self.result delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [friendListAlertView show];
        [friendListAlertView release];
    }else {
        self.reportList = (NSMutableArray*) self.result;
        NSLog(@"%d",[reportList count]);
    }
}

这是我最后一次尝试提供帮助:-(

Since you are not calling the method by thread you don't have to use an autorelease pool. Can you try this following code.

-(void)getGraphicalData{
    self.friendBL = [[[FriendBL alloc] init] autorelease];
    self.result = [self.friendBL  getGraphicalData1:txtStartTime.text andTodate:txtEndTime.text];
    if ([self.result isKindOfClass:[NSString class]]) {
        UIAlertView * friendListAlertView = [[UIAlertView alloc] initWithTitle:@"Friend List" message:(NSString*) self.result delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [friendListAlertView show];
        [friendListAlertView release];
    }else {
        self.reportList = (NSMutableArray*) self.result;
        NSLog(@"%d",[reportList count]);
    }
}

This is my last try to help :-(

但可醉心 2024-11-26 11:35:52

您有一个数组,其中没有任何对象,并且您正在尝试访问第一个对象。如果您向我们展示代码,我们可以更具体。

You have an array without any objects in it and you're attempting to access the first object. If you show us code we can be more specific.

ゝ杯具 2024-11-26 11:35:52

在上面的函数中,您没有释放对象 FriendBL。您如何存储您的财产?保留、分配还是复制?

可能是自动释放池的问题。该函数是在主线程上调用的吗?

如果您能给我们更多的见解,这可能会有用。

In the above function you are not releasing the object FriendBL. How are you storing your property? Retain or assign or copy ?

Might be the problem with autorelease pool. Is the function called on a main thread ?

If you can give us more insight it might be useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文