Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 840
Tattat Avatar asked Apr 21 '10 15:04

Tattat


People also ask

What is difference between NSArray and NSMutableArray?

The primary difference between NSArray and NSMutableArray is that a mutable array can be changed/modified after it has been allocated and initialized, whereas an immutable array, NSArray , cannot.

What is NSMutableArray Objective C?

The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray . NSMutableArray is “toll-free bridged” with its Core Foundation counterpart, CFMutableArray .


2 Answers

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: How can I add CGPoint objects to an NSArray the easy way?

like image 147
kharrison Avatar answered Sep 22 '22 18:09

kharrison


You can also do the following:

[myArray addObject:[NSValue valueWithCGPoint:MyCGPoint]]; 
like image 40
atbreuer11 Avatar answered Sep 24 '22 18:09

atbreuer11