How to store CGPoint in an array

Hi, I'm trying to save move points in NSMutableArray , so I have such attempts

 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *move = [[event allTouches] anyObject]; CGPoint MovePoint = [move locationInView:self.view]; if (MovePointsArray==NULL) { MovePointsArray=[[NSMutableArray alloc]init]; } [MovePointsArray arrayWithObjects:[NSValue valueWithCGPoint:MovePoint]]; } 

but it doesn’t work, how can I save these points in NSMutableArray

+6
source share
3 answers

You should use addObject: in the last line:

 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *move = [[event allTouches] anyObject]; CGPoint MovePoint = [move locationInView:self.view]; if (MovePointsArray==NULL) { MovePointsArray=[[NSMutableArray alloc]init]; } [MovePointsArray addObject:[NSValue valueWithCGPoint:MovePoint]]; } 
+17
source

You need to do the following:

 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *move = [[event allTouches] anyObject]; CGPoint MovePoint = [move locationInView:self.view]; if (MovePointsArray == NULL) { MovePointsArray = [[NSMutableArray arrayWithObjects:[NSValue valueWithCGPoint:MovePoint, nil]; } else { [MovePointsArray addObject:[NSValue valueWithCGPoint:MovePoint]]; } } 

do not forget to save / pass the array, as you do not see, to use the accessor property.

Best of all, you should allocate / initialize the array in your init method, and then only here:

 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *move = [[event allTouches] anyObject]; CGPoint MovePoint = [move locationInView:self.view]; [MovePointsArray addObject:[NSValue valueWithCGPoint:MovePoint]]; } 
+2
source

If you want to get an array using the arrayWithObjects method, you must also add nil as the last element of the array.

So:

 [MovePointsArray arrayWithObjects:[NSValue valueWithCGPoint:MovePoint], nil]; 

but to add an object to an existing array, you must use the addObject method

 [MovePointsArray addObject:[NSValue valueWithCGPoint:MovePoint]]; 
+1
source

Source: https://habr.com/ru/post/898833/


All Articles