Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to store CGPoint in array

Hi I am trying to store the move points in the NSMutableArray so I have tries like this

-(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 this doesn't work how can i store these points in NSMutableArray

like image 282
Nik's Avatar asked Oct 07 '11 14:10

Nik's


2 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]];
}
like image 185
justadreamer Avatar answered Nov 02 '22 06:11

justadreamer


You should do like this :

-(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]];
    }
}

don't forget to retain / relase the array as you don't see to use a property accessor.

Best, you should alloc/init the array in your init method and then only do here :

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *move = [[event allTouches] anyObject];
    CGPoint MovePoint = [move locationInView:self.view];

    [MovePointsArray addObject:[NSValue valueWithCGPoint:MovePoint]];
}
like image 2
Oliver Avatar answered Nov 02 '22 08:11

Oliver