2017-02-17 72 views
0

我正在开发iOS应用程序,当我运行应用程序时我的应用程序。 App Run完美。但数组长度不计入每个。如何计算每个数组的长度。提前致谢。NSArrayI objectAtIndex:]:索引7超越边界[0 .. 6]

这是错误..

'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++; 
    } 
} 
+0

是最后的代码或部分代码在例外? – Vishnuvardhan

+0

每当您的计时器触发时,您会增加'count',并且当计数超过阵列的大小时,您不会停止计时器或重置计数 – Paulw11

回答

0

调用updateLocation方法定时器已经设定repeats:YES所以这将每2秒被调用。

您在updateLocation方法中正在递增for循环中的计数count++;。但是,您只需在viewDidLoad中重置计数一次。所以第一次调用会运行得很好,但下一次调用会失败,索引越界。

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

0

重置count到0之前的foreach循环,使你的代码将是这样的:

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++; 
} 
相关问题