【问题标题】:NSArrayI objectAtIndex:]: index 7 beyond bounds [0 .. 6]NSArrayI objectAtIndex:]: 索引 7 超出范围 [0 .. 6]
【发布时间】:2017-02-17 11:46:06
【问题描述】:

当我运行应用程序时,我正在我的应用程序中开发 IOS App.in。应用运行完美。但数组长度不计入 For each。如何计算每个数组的长度。提前致谢。

这是错误..

'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 7 beyond bounds [0 .. 6]'

代码

- (void)viewDidLoad {
    [super viewDidLoad];
    count = 0;
    marker = [[GMSMarker alloc] init];
    marker.position = camera.target;
    marker.snippet = @"Jalandhar";
    marker.map = mapView;
    self.view = mapView;
    timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(updateLocation:) userInfo:nil repeats:YES];
}

- (void)updateLocation:(NSTimer *)timer {
    [mapView clear];

    NSMutableArray *longitute = [[NSMutableArray alloc]init];
    NSDictionary *latLongDict = @{@"lat": @[@"31.3062", @"31.3107",@"31.3102",@"31.3194",@"31.3312",@"29.9083",@"31.2941",],@"long": @[@"75.5935", @"75.6061",@"75.6117",@"75.5845",@"75.5908",@"75.7299",@"75.5844",]};
    [longitute addObject:latLongDict];

    for (NSDictionary *dic in longitute) {
        NSString *val = [[dic valueForKey:@"lat"]objectAtIndex:count];
        NSString *value = [[dic valueForKey:@"long"]objectAtIndex:count];
        NSLog(@"%@",val);
        NSLog(@"%@",value);
        [CATransaction begin];
        [CATransaction setValue:[NSNumber numberWithFloat: 2.0] forKey:kCATransactionAnimationDuration];

        CLLocationCoordinate2D center;
        center.latitude=[val doubleValue];
        center.longitude=[value doubleValue];
        marker = [[GMSMarker alloc] init];
        marker.position = center;
        marker.map = mapView;
        self.view = mapView;
        [CATransaction commit];
        count++;
    }
}

【问题讨论】:

  • 最终代码还是部分代码异常?
  • 每次你的定时器触发你增加count并且当它超过你的数组大小时你不会停止定时器或重置count

标签: ios objective-c nsarray


【解决方案1】:

调用updateLocation方法的定时器设置了repeats:YES,所以它将每2秒调用一次。

您在 updateLocation 方法中的 for 循环中增加计数 count++;。但是,您只在viewDidLoad 中重置了一次计数。所以第一次调用会运行得很好,但下一次会因为索引超出范围而失败。

updateLocation方法中在[map clear]下方添加count = 0;来解决问题

【讨论】:

    【解决方案2】:

    foreach 循环之前将count 重置为0,这样您的代码将如下所示:

    count = 0 // Reset count to zero before the foreach loop
    for (NSDictionary *dic in longitute) {
            NSString *val = [[dic valueForKey:@"lat"]objectAtIndex:count];
            NSString *value = [[dic valueForKey:@"long"]objectAtIndex:count];
            NSLog(@"%@",val);
            NSLog(@"%@",value);
            [CATransaction begin];
            [CATransaction setValue:[NSNumber numberWithFloat: 2.0] forKey:kCATransactionAnimationDuration];
    
            CLLocationCoordinate2D center;
            center.latitude=[val doubleValue];
            center.longitude=[value doubleValue];
            marker = [[GMSMarker alloc] init];
            marker.position = center;
            marker.map = mapView;
            self.view = mapView;
            [CATransaction commit];
            count++;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-11
      相关资源
      最近更新 更多