I have an Objective-C method which uses some x and y values from an image: image.center.x
and image.center.y
. I want to store them away every time this method is called, so I was hoping to use an array.
How can this be done? I suspect using an NSMutableArray?
C arrays are a proper subset of Objective C, as well as producing faster code and often using less memory than using Cocoa Foundation classes. You could add:
CGPoint myPoints[MAX_NUMBER_OF_POINTS];
to your instance variables; and save coordinates with:
myPoints[i] = image.center;
I would recommend storing the points in an NSArray, wrapped using NSValue:
NSMutableArray *arrayOfPoints = [[NSMutableArray alloc] init];
[arrayOfPoints addObject:[NSValue valueWithCGPoint:image.center]];
// Do something with the array
[arrayOfPoints release];
This assumes that image.center
is a CGPoint struct (if not, you can make one using CGPointMake()
).
To extract the CGPoint, simply use
[[arrayOfPoints objectAtIndex:0] CGPointValue];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With