I am trying to store CGPoints from UITouches in an Array. From reading other threads I have realized that using NSValue seems to be the best way to do this.
Currently I have:
NSMutableArray *dotsArray;
NSValue *pointObj;
UITouch *touch = obj;
CGPoint touchPoint = [touch locationInView:self.view];
// Need to store the CGPoints, can't store directly into an array // as it is a C struct // so we use NSValue
// CGPoint converted to NSValue
CGPoint point = touchPoint;
pointObj = [NSValue valueWithCGPoint:point];
dotsArray = [NSArray arrayWithObjects:pointObj,nil];
// Print to console the objects in the collection
NSLog(@"array content: %@", dotsArray);
NSLog(@"[dotsArray count] = %i",[dotsArray count]);
// Restore from NSValue to C structures
CGPoint pointRestored = [pointObj CGPointValue];
I want to be able to use:
[dotsArray insertObject:pointObj atIndex:0];
in place of
dotsArray = [NSArray arrayWithObjects:pointObj,nil];
so that I can increment the placing as I recall the method to store multiple points.
From reading I think this has something to do with the ID type required for the ‘insertObject’, but I don’t fully understand it and am unable to find a solution online.
EDIT: When I use
[dotsArray insertObject:pointObj atIndex:0];
then
NSLog(@"array content: %@", dotsArray);
outputs NULL
Use
addObject:method.I think it will be helpful to you.