How do I add a CGPoint to NSMutableArray?

前端 未结 6 1778
小蘑菇
小蘑菇 2021-02-01 13:58

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

相关标签:
6条回答
  • 2021-02-01 14:12

    You can also do the following:

    [myArray addObject:[NSValue valueWithCGPoint:MyCGPoint]];
    
    0 讨论(0)
  • 2021-02-01 14:17

    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.

    0 讨论(0)
  • 2021-02-01 14:20

    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.

    0 讨论(0)
  • 2021-02-01 14:30

    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?

    0 讨论(0)
  • 2021-02-01 14:31

    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 讨论(0)
  • 2021-02-01 14:32

    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");
    
    0 讨论(0)
提交回复
热议问题