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?
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.
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 .
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?
You can also do the following:
[myArray addObject:[NSValue valueWithCGPoint:MyCGPoint]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With