I'm having a weird error popping up. It's listing that my app is

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

What puzzles me is that the app runs perfectly fine in the simulator, but when I go to test it on my device I get that error. I've isolated the problem to the line of code below:

updatingTracker = [userDatas objectAtIndex:repeatCount];

Here is the rest of the relevant code:

[self getUserDatas];
[self timerDidFire];

-(void) getUserDatas
{
    userDatas = [[NSMutableArray alloc] init];
    NSUserDefaults *userdef = [NSUserDefaults standardUserDefaults];
    int count = [[userdef objectForKey:@"user_count"] intValue];
    for(int i=0;i<count;i++)
    {
        NewTracker *newtrack=[[NewTracker alloc] init];
        newtrack.FromCurrency = [userdef objectForKey:[NSString stringWithFormat:@"from_string_%d",i]];
        newtrack.ToCurrency   = [userdef objectForKey:[NSString stringWithFormat:@"to_string_%d",i]];
        newtrack.savedCurrency = [userdef objectForKey:[NSString stringWithFormat:@"save_currency_%d",i]];
        newtrack.userTarget = [userdef objectForKey:[NSString stringWithFormat:@"user_target_%d",i]];
        newtrack.trackerId = [userdef objectForKey:[NSString stringWithFormat:@"tracker_id_%d",i]];
        newtrack.realtimeBase = [userdef objectForKey:[NSString stringWithFormat:@"realtime_base_%d",i]];
        newtrack.realtimeTarget = [userdef objectForKey:[NSString stringWithFormat:@"realtime_target_%d",i]];
        newtrack.realtimeUSD = [userdef objectForKey:[NSString stringWithFormat:@"realtime_USD_%d",i]];
        [userDatas addObject:newtrack];
    }
}

- (void) timerDidFire
{
    NSLog(@"bug finder");

    updatingTracker = [userDatas objectAtIndex:repeatCount];
    [self chartconnectionStart:@"3m"];
}

Any ideas or help on why this is happening or how to fix it would be appreciated.

有帮助吗?

解决方案

Your userDatas array is empty. You most likely don't have anything populated in your user defaults on your phone, so the code that adds to the array is never executed. You probably have some data already populated on your simulator and that's why it's not working on the phone

其他提示

Simple put a conditional breakpoint on that line for the condition [userDatas count] == 0. Or put in an if to catch that and a breakpoint on it. They use the debugger to figure out how you got there and what the associated values are.

Basic debugging.

Code like this

  • (void) timerDidFire{

    NSLog(@"bug finder");

    if([userDatas count]>0){

    updatingTracker = [userDatas objectAtIndex:repeatCount];

    [self chartconnectionStart:@"3m"];

    }

}

Happy coding..........

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top