0

我的应用程序出现了一些问题:当您玩游戏并且时间结束时,服务器会发送您的分数。当互联网关闭时,应用程序仍然发送请求,当互联网再次打开时,应用程序崩溃。控制台向我展示了这个:

2013-05-31 11:00:34.376 xxxxxxx [1721:1be03] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(0x3352012 0x2754e7e 0x32f40b4 0xad260 0xacc1b 0xacb95 0xa770c 0x29ce3 0xa743b 0x68e04 0x68b0b 0x874b8 0x27686b0 0x1e1f765 0x32d5f3f 0x32d596f 0x32f8734 0x32f7f44 0x32f7e1b 0x358a7e3 0x358a668 0x1323ffc 0x65a9a 0x28e5 0x1)
libc++abi.dylib: terminate called throwing an exception

有人可以告诉我出了什么问题吗?

编辑:

我放了一个异常断点,发现问题出在这里。这是服务器响应的解析,用管道分隔:

-(void)parseNextGameScoresStatWithResponse:(NSString *)response{
    /*Response
     Position|username|totalscore|country|
     */
    if(response.length == 0 )
        return;

    NSString * cuttedString = [response substringFromIndex:1];

    NSMutableArray *responsesArray = [NSMutableArray arrayWithArray:[cuttedString componentsSeparatedByString:@"|"]];

    if(responsesArray.count != 0)
       [responsesArray removeLastObject];
    else{
        return;
    }

  //  NSLog(@"responsesArray = %@", responsesArray);

    self.statsArray = [NSMutableArray arrayWithCapacity:0];

    for (int i = 0; i < [responsesArray count]-1; i+=4) {
        StatModel *stat = [[StatModel alloc] init];
        stat.position = [[responsesArray objectAtIndex:i] intValue];
        stat.userName = [responsesArray objectAtIndex:i+1];
        stat.totalScore = [[responsesArray objectAtIndex:i+2] intValue];
        stat.countryCode = [responsesArray objectAtIndex:i+3];
  //      NSLog(@"stat of next game scores = %d %@ %d %@",stat.position, stat.userName, stat.totalScore, stat.countryCode);
        [self.statsArray addObject:stat];
        [stat release];
    }
}
4

1 回答 1

1

问题是即使[responseArray count]is 0,它仍然会进入 for 循环。

改变你的循环:

for (int i = 0; i < [responsesArray count]-1; i+=4) {
  ..

到:

int i = 0;
while (i < [responsesArray count]) {
    StatModel *stat = [[StatModel alloc] init];
    stat.position = [[responsesArray objectAtIndex:i++] intValue];
    stat.userName = [responsesArray objectAtIndex:i++];
    stat.totalScore = [[responsesArray objectAtIndex:i++] intValue];
    stat.countryCode = [responsesArray objectAtIndex:i++];
    [self.statsArray addObject:stat];
    [stat release];
}
于 2013-05-31T14:51:09.923 回答