There are two questions: Errors in your code and exception.
Code errors
You add an overlay for all sumaCount points until the puntitos structure is puntitos (for example, when addOverlay is called for the first time, you add only [a1 count] tags). You also think that a1 , a2 and a3 are all elements in rutas . If you want them to be connected, you must:
- Extract
addOverlay from the loop; and - Do not use
a1 , a2 and a3 , but rather repeat through rutas to get sumaCount
Thus, if you really wanted to join the three groups of lines, you would:
int sumaCount = 0; for (NSArray *array in rutas) sumaCount += [array count]; CLLocationCoordinate2D puntitos[sumaCount]; int c = 0; for (NSArray *array in rutas) { for (CLLocation *cada in array) { puntitos[c] = CLLocationCoordinate2DMake(cada.coordinate.latitude, cada.coordinate.longitude); c++; } } self.routeLine = [MKPolyline polylineWithCoordinates: puntitos count: sumaCount]; [self.mapView addOverlay: self.routeLine];
If you are dealing with three separate row groups, you should:
for (NSArray *array in rutas) { int sumaCount = [array count]; CLLocationCoordinate2D puntitos[sumaCount]; int c = 0; for (CLLocation *cada in array) { puntitos[c] = CLLocationCoordinate2DMake(cada.coordinate.latitude, cada.coordinate.longitude); c++; } MKPolyline routeLine = [MKPolyline polylineWithCoordinates: puntitos count: c]; [self.mapView addOverlay: routeLine]; }
Note that in this last example, I am not using your MKPolyline property, but the local var. If you need to save an array of these MKPolyline objects, just go ahead and do it, but for the purposes of the above code you don't need it. Honestly, in the first example, I probably lean towards you with the local variable MKPolyline . Why store it in a class property?!?
Exception
The above bug fixes are in your code, but your exception indicates another issue. This may be your code (since your first attempt to create MKPolyline uses sumaCount points, but only some of them). But the problem may be reset elsewhere, because the exception is not what you expect from code errors. Are you regexing elsewhere in your code? If you are sure that the problem is in the code itself, you can
NSLog(@"rutas=%@", rutas);
Personally, I would be surprised if the regex problem was caused by this, but the code in your original question could definitely cause some unexpected problems. I will fix the code and see if your exception is still happening. If so, add this rutas log statement, but better, find NSRegularExpression in your project and see where you can use it. You can also enable exception breakpoints (just enable it for all exceptions).