Storing and retrieving CGPoints inside an NSMutableArray

I looked through countless questions here and elsewhere and I canโ€™t understand for my life what I am doing wrong.

I am trying to save an array of CGPoints as NSValues โ€‹โ€‹inside an NSMutableArray called such points on the iPhone:

NSValue *point = [NSValue valueWithCGPoint:firstTouch]; NSLog(@"NSValue *point = %@", point); [points addObject:point]; NSLOG OUTPUT NSValue *point = NSPoint: {120, 221} 

Everything goes smoothly from CGPoint to NSValue. But when I try to get the point, I get nothing.

 NSValue *getPoint = [points objectAtIndex:0]; CGPoint thePoint = [getPoint CGPointValue]; NSLog(@"Point = %@", NSStringFromCGPoint(thePoint)); NSLOG OUTPUT Point = {0, 0} 

The points should be the same, but I get a zero result.

For testing purposes, this happens in the touchhesBegan method.

Does anyone know where I am going wrong? Thanks in advance.

+4
source share
3 answers

I never allocated my array to memory or initialized it. My points were not stored in the array because there was no array that existed.

+2
source

to add CGPoint to NSMutableArray

 [ArrObj addObject:NSStringFromCGPoint(PointObj)]; 

to get CGPoint from NSMutableArray

 CGPoint pointObj2 = CGPointFromString([ArrObj objectAtIndex:index]); 

can it help you.

+1
source

First allocate your modified array in memory:

 points = [[NSMutableArray alloc] init]; 

Then insert.

-1
source

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


All Articles