johnsonjp34 johnsonjp34 - 3 years ago 88
iOS Question

Reversing a GMSMutablePath Array

According to the documentation GMSMutablePath is:

GMSMutablePath is a dynamic (resizable) array of CLLocationCoordinate2D. [Google Documentation]

I want to reverse the order of the Path coordinates. Normally with an Array I would use:

[[array123 reverseObjectEnumerator] allObjects];

but none of the NSArray functions will work on it and if I try to cast it to an NSArray or NSMutableArray I just get red flags.

How do I reverse the order of the elements in GMSMutablePath or how do I properly cast it to a NSArray?

Answer Source

GMSMutablePath doesn't inherits from NSMutableArray, so you can't cast it as a NSMutableArray. What you can do it do it manually. If we wanted to do it with NSMutableArray we could call exchangeObjectAtIndex:withObjectAtIndex:, but since GMSMutablePath doesn't seems to provide an equivalent for that method, we can do it more explictly.

for (NSUInteger i1 = 0; i1 < [myPath count]/2; i1 ++)
   NSUInteger i2 =  [myPath count]-i1;
   CLLocationCoordinate2D *coord1 = [myPath coordinateAtIndex:i1];
   CLLocationCoordinate2D *coord2 = [myPath coordinateAtIndex:i2];
   [myPath replaceCoordinateAtIndex:i1 withCoordinate:coord2];
   [myPath replaceCoordinateAtIndex:i2 withCoordinate:coord1];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download