4

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...

Jeremy L
  • 3,770
  • 6
  • 41
  • 62

3 Answers3

10

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];
sch
  • 27,436
  • 3
  • 68
  • 83
6

You should use the NSValue class with the following methods:

+ valueWithCGPoint:
- CGPointValue
Hampus Nilsson
  • 6,692
  • 1
  • 25
  • 29
3

You have to choose between...

A. c pointer array (look here)

or

B. NSValue or your NSStringFromCGPoint. Like this: [NSValue valueWithCGPoint:CGPointMake(5.5, 6.6)]

Community
  • 1
  • 1
Jonas Schnelli
  • 9,965
  • 3
  • 48
  • 60