Search code examples
objective-cnsmutablearraynsarray

Most efficient way of changing the value of a CGPoint in a CGPoint array?


I have constructed a CG Point array as follows:

NSMutableArray *_startPositions = [NSMutableArray array];
[_startPositions addObject:[NSValue valueWithCGPoint:CGPointMake(0.0, 0.0)]];
[_startPositions addObject:[NSValue valueWithCGPoint:CGPointMake(0.0, 0.0)]];

How can i most efficiently change the x and y values of the points in the array?

If I retrieve the value using:

CGPoint myPoint0 = [[_startPositions objectAtIndex:0] CGPointValue];

I get back a copy of the point. How can I change the x and y values of the CGPoints in the array? I could remove the CGPoint at any position and replace it with one with the correct x,y values, but is there a more efficient way?


Solution

  • Use C array instead of NSMutableArray. If required use C++ STL collection classes such as std::vector or other C/C++ collection library. In this way, you can manipulate the array content without any Obj-C method call.