How do I add a CGPoint to NSMutableArray?
Asked Answered
K

6

41

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?

Karttikeya answered 21/4, 2010 at 15:52 Comment(1)
L
36

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?

Lafayette answered 21/4, 2010 at 16:29 Comment(1)
Consider difference between two methods: valueWithCGPoint: and valueWithPointer:. Details hereMesoblast
P
31

You can also do the following:

[myArray addObject:[NSValue valueWithCGPoint:MyCGPoint]];
Parallelize answered 12/7, 2013 at 16:24 Comment(0)
T
11

Unfortunately for you a CGPoint isn't an Objective-c object. It is a c struct. if you Apple double click on CGPoint you should jump to the definition

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

If you want to store CGPoint in an NSArray you will need to wrap them first. You can use NSValue for this or write your own wrapper.

see Converting a CGPoint to NSValue

EDIT> There is a small overhead for each objective-c method call, and creating and destroying objects involves many method calls before they are even used for anything. You shouldn't worry about this normally but for very small objects which encapsulate little behaviour and that have short lifetimes it can affect performance. If Apple used objects for all points, rect, sizes and even ints, floats, etc performance would be worse.

Toadstool answered 21/4, 2010 at 16:28 Comment(0)
R
4

To build on the answer given by atbreuer11, you can convert your CGPoint to NSValue, store it in NSMutableArray and convert it 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
}

That's probably the easiest way to do it. You can write a complete class and do all types of data manipulation, but I think it would be an overkill, unless you really have to.

EDIT

For Swift 5 (I'm not sure why one would want to do this, given that we can use literal arrays nowadays, but here goes):

Save Values:

let somePoint = CGPoint(x: 200, y: 400)
let array = NSMutableArray(array: [somePoint])

To retrieve it:

let points = array.compactMap({ ($0 as? NSValue)?.cgPointValue })
Riles answered 25/10, 2015 at 15:52 Comment(0)
A
0

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
}
Arlenarlena answered 14/7, 2017 at 7:25 Comment(0)
H
-1

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

The code is longer, but clean. An example is below:

In .h file:

@interface MyPoint:NSObject
{
     CGPoint myPoint;   
}

- (id) init;
- (id) Init:(CGPoint) point;
- (BOOL)isEqual:(id)anObject;

@end

In .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 some code sample showing the usage, do not forget to 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");
Humpbacked answered 16/7, 2012 at 1:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.