我弹出一个奇怪的错误。它列出了我的应用
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
让我感到困惑的是,该应用程序在模拟器中运行得非常好,但是当我在我的设备上进行测试时,我得到了这个错误。我已将问题隔离到以下代码行:
updatingTracker = [userDatas objectAtIndex:repeatCount];
以下是相关代码的其余部分:
[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"];
}
对于为什么会发生这种情况或如何解决问题的任何想法或帮助将不胜感激。
答案 0 :(得分:0)
您的userDatas数组为空。您很可能在手机上的用户默认设置中没有填充任何内容,因此添加到阵列的代码永远不会执行。您可能已经在模拟器上填充了一些数据,这就是为什么它不能在手机上工作
答案 1 :(得分:0)
简单地在条件[userDatas count] == 0
的该行上放置一个条件断点。或者输入一个if
来捕获它和断点。他们使用调试器来弄清楚你是如何到达那里以及相关的值是什么。
基本调试。
答案 2 :(得分:0)
像这样的代码
(void)timerDidFire {
NSLog(@“bug finder”);
if([userDatas count]&gt; 0){
updatingTracker = [userDatas objectAtIndex:repeatCount];
[self chartconnectionStart:@“3m”];
}
}
快乐的编码..........