Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For iPad / iPhone apps, how to have an array of points?

We can have an NSMutableArray object, and add objects to it.

But CGPoint is not an object... are there Point objects suitable to be added to an NSMutableArray object?

I see some code using NSStringFromCGPoint to create an NSString object so that it can be added to the array, and later use CGPointFromString to get the CGPoint back... but that just looks too much of a hack...

like image 332
Jeremy L Avatar asked Dec 02 '22 00:12

Jeremy L


2 Answers

You can store the points in the array using NSValue as a wrapper:

CGPoint a = CGPointMake(10.0, 10.0);
[array addObject:[NSValue valueWithCGPoint:a]];

CGPoint b = [(NSValue *)[array objectAtIndex:0] CGPointValue];
like image 135
sch Avatar answered Dec 05 '22 10:12

sch


You should use the NSValue class with the following methods:

+ valueWithCGPoint:
- CGPointValue
like image 26
Hampus Nilsson Avatar answered Dec 05 '22 08:12

Hampus Nilsson