是什么导致此IndexOutOfBoudeException?

时间:2019-05-17 13:42:47

标签: objective-c

我一直在获取此indexOutOfBoundsException。我不知道是什么原因造成的。

我尝试了各种数组类型,也定义了数组的容量,但是我一直遇到错误。

我设置了一个异常断点,该断点显示导致此问题的这一行。

问题专线?

NSDictionary *subObject = [timprofiles objectAtIndex:i];

代码:

for(int i = 0; i < timprofiles.count; i++){

    NSString *tziid = @"";
    NSString *serial = @"";
    NSString *to_time = @"";
    NSString *from_time = @"";
    NSString *monday = @"";
    NSString *tueday = @"";
    NSString *wednesday = @"";
    NSString *thursday = @"";
    NSString *friday = @"";
    NSString *saturday = @"";
    NSString *sunday = @"";
    NSString *holiday = @"";
    NSString *special_day_one = @"";
    NSString *special_day_two = @"";

    NSDictionary *subObject = [timprofiles objectAtIndex:i];

    NSString *tzid =  [subObject objectForKey:@"TZID"];
    NSString *tzitemid =  [subObject objectForKey:@"TZITEMID"];


     NSArray *timeprofiledegments = [subObject objectForKey:@"TIMEPROFILESUBSEGS"];

 //   NSLog(@"time profile segments %@", timeprofiledegments);

    for(int x = 0; x < timeprofiledegments.count; x++){


        NSDictionary *arryObjects = [timeprofiledegments objectAtIndex:i];

        tziid = [arryObjects objectForKey:@"TZIID"];
        serial = [arryObjects objectForKey:@"SERIAL"];


        NSString *timeSegList = [arryObjects objectForKey:@"TIMESLICE"];
        NSArray *splitarray = [timeSegList componentsSeparatedByString:@";"];


        for (int j = 0; j < splitarray.count; j++) {
            switch (j) {
                case 0:
                    from_time = [splitarray objectAtIndex:j];
                    break;
                case 1:
                    to_time = [splitarray objectAtIndex:j];
                    break;
                case 2:
                    monday = [splitarray objectAtIndex:j];
                    break;
                case 3:
                    tueday = [splitarray objectAtIndex:j];
                    break;
                case 4:
                    wednesday = [splitarray objectAtIndex:j];
                    break;
                case 5:
                    thursday = [splitarray objectAtIndex:j];
                    break;
                case 6:
                    friday = [splitarray objectAtIndex:j];
                    break;
                case 7:
                    saturday = [splitarray objectAtIndex:j];
                    break;
                case 8:
                    sunday = [splitarray objectAtIndex:j];
                    break;
                case 9:
                    holiday = [splitarray objectAtIndex:j];
                    break;
                case 10:
                    special_day_one = [splitarray objectAtIndex:j];
                    break;
                case 11:
                    special_day_two = [splitarray objectAtIndex:j];
                    break;
                default:
                    break;
            }

             [timeProfilesList addObject:[NSString stringWithFormat:@"%@",tziid]];
             [timeProfilesList addObject:[NSString stringWithFormat:@"%@",tzid]];
             [timeProfilesList addObject:[NSString stringWithFormat:@"%@",serial]];
             [timeProfilesList addObject:[NSString stringWithFormat:@"%@",from_time]];
             [timeProfilesList addObject:[NSString stringWithFormat:@"%@",to_time]];
             [timeProfilesList addObject:[NSString stringWithFormat:@"%@",monday]];
             [timeProfilesList addObject:[NSString stringWithFormat:@"%@",tueday]];
             [timeProfilesList addObject:[NSString stringWithFormat:@"%@",wednesday]];
             [timeProfilesList addObject:[NSString stringWithFormat:@"%@",thursday]];
     }

1 个答案:

答案 0 :(得分:2)

我认为该错误不会在您提到的行中发生,而应该在行中发生:

NSDictionary *arryObjects = [timeprofiledegments objectAtIndex:i];

因为您使用i作为迭代器,而应该使用x

使用for-each迭代器可以避免整个过程:

for (NSDictionary *subObject in timprofiles) {
   ...
   for (NSDictionary *arryObjects in timeprofiledegments) {

还要注意您的

[timeProfilesList addObject:...]

应该位于最里面的for之外,并且不必调用stringWithFormat,因为您的变量已经是NSString