How to add CGPoint to NSMutableArray?

I want to save my CGPoint in an NSMutable Array, so I have a method like this:

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

But I got an error, he said that:

Incompatible type for argument 1 of "AddObject".

So, I am checking the API,

 - (void)addObject:(id)anObject 

anObject An object to add to the end of the contents of the receiver. This value should not be nil.

So, I think that CGPointMake can create an object, but it cannot be assigned. What is happening?

+41
objective-c iphone nsarray cgpoint
Apr 21 '10 at 15:52
source share
6 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 an iPhone, you can use the NSValue UIKit add-ons to convert CGPoint to an NSValue object.

See the previous answer for examples: How can I add CGPoint objects to NSArray in a simple way?

+35
Apr 21 '10 at 16:29
source share

You can also do the following:

 [myArray addObject:[NSValue valueWithCGPoint:MyCGPoint]]; 
+30
Jul 12 '13 at 16:24
source share

Unfortunately, for you, CGPoint is not an Objective-c object. This is a c-structure. if you double-click Apple on CGPoint, you must go to the definition

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

If you want to save CGPoint to NSArray, you need to wrap them first. You can use NSValue for this or write your own wrapper.

see Convert CGPoint to NSValue

EDIT> There is a small overhead for each call to the Objective-c method, and creating and destroying objects involves many method calls before they are even used for anything. You should not worry about this, as a rule, but for very small objects that encapsulate little behavior and have a short life, this can affect performance. If Apple used objects for all points, corrections, sizes and even functions int, floats, etc. It would be worse.

+11
Apr 21 '10 at 16:28
source share

To build the response received by atbreuer11, you can convert CGPoint to NSValue, save it to NSMutableArray and convert back using the following:

 //Convert CGPoint and Store it CGPoint pointToConvert = CGPointMake(100.0f, 100.0f); NSValue *valueToStore = [NSValue valueWithCGPoint:pointToConvert]; NSMutableArray *arrayToKeep =[NSMutableArray arrayWithObject:valueToStore]; 

Then restore it again:

 CGPoint takeMeBack; for (NSValue *valuetoGetBack in arrayToKeep) { takeMeBack = [valuetoGetBack CGPointValue]; //do something with the CGPoint } 

This is probably the easiest way to do this. You can write a complete class and do all kinds of data manipulations, but I think it will be redundant if you really don't need to.

+3
Oct 25 '15 at 15:52
source share

Swift 3.x
// Convert CGPoint to NSValue

 let cgPoint = CGPoint(x: 101.4, y: 101.0) let nsValue = NSValue(cgPoint: cgPoint) var array = NSArray(object: nsValue) 

// Restore It Again

 var cgPoint : CGPoint! for i in array { cgPoint = i as? CGPoint } 
0
Jul 14 '17 at 7:25
source share

An CGPoint way to handle CGPoint (or any other inherited non- NSObject structure) is to create a new class inherited from NSObject .

The code is longer but clean. The following is an example:

In the .h file:

 @interface MyPoint:NSObject { CGPoint myPoint; } - (id) init; - (id) Init:(CGPoint) point; - (BOOL)isEqual:(id)anObject; @end 

In the .m file:

 @implementation MyPoint - (id) init { self = [super init]; myPoint = CGPointZero; return self; } - (id) Init:(CGPoint) point{ myPoint.x = point.x; myPoint.y = point.y; return self; } - (BOOL)isEqual:(id)anObject { MyPoint * point = (MyPoint*) anObject; return CGPointEqualToPoint(myPoint, point->myPoint); } @end 

Here is a sample code showing usage, don't forget release !!!

 //init the array NSMutableArray *pPoints; pPoints = [[NSMutableArray alloc] init]; // init a point MyPoint *Point1 = [[MyPoint alloc]Init:CGPointMake(1, 1)]; // add the point to the array [pPoints addObject:[[MyPoint alloc] Point1]]; //add another point [Point1 Init:CGPointMake(10, 10)]; [pPoints addObject:[[MyPoint alloc] Point1]]; [Point1 Init:CGPointMake(3, 3)]; if ([pPoints Point1] == NO)) NSLog(@"Point (3,3) is not in the array"); [Point1 Init:CGPointMake(1, 1)]; if ([pPoints Point1] == YES)) NSLog(@"Point (1,1) is in the array"); 
-one
Jul 16 2018-12-12T00:
source share



All Articles