Tattat Tattat - 1 year ago 66
Objective-C Question

How do I add a CGPoint to NSMutableArray?

I want to store my CGPoint to the NSMutable Array, so , I have method like this:

[self.points addObject:CGPointMake(x, y)];

But I got the error, it said that :

Incompatible type for argument 1 of

So, I check out the API,

- (void)addObject:(id)anObject

anObject The object to add to the end
of the receiver's content. This value
must not be nil.

So, I think the "CGPointMake" can make a Object, but it can't be assigned. What happens?

Answer Source

The problem is that CGPoint is actually just a C structure it is not an object:

struct CGPoint {
   CGFloat x;
   CGFloat y;
typedef struct CGPoint CGPoint;

If you are on the iPhone you can use the NSValue UIKit additions to convert the CGPoint to an NSValue object.

See this previous answer for examples: http://stackoverflow.com/questions/899600/how-can-i-add-cgpoint-objects-to-an-nsarray-the-easy-way