2017-02-17 8 views
0

私はIOSアプリケーションを開発しています。アプリは完璧に走ります。しかし、配列の長さはFor eachでカウントされません。それぞれの配列の長さを数える方法。前もって感謝します。NSArrayI objectAtIndex:]:インデックス7の境界を超えて[0 .. 6]

これはエラー..ですupdateLocationメソッドを呼び出します

'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'をインクリメントし、タイマーを止めたり、配列のサイズを超えたときに' count'をリセットすることはありません – Paulw11

答えて

0

タイマーは、それが2秒ごとに呼び出されますrepeats:YESを設定しました。

メソッドでは、forループ内のカウントを増やしています(count++;)。ただし、viewDidLoadで1回だけカウントをリセットします。したがって、最初の呼び出しは正常に実行されますが、次の呼び出しはインデックスの範囲外で失敗します。

あなたのコードは次のようになりますので

0

がちょうどforeachループの前count 0にリセット問題を修正するためにupdateLocation方法で[map clear]以下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++; 
} 
関連する問題