How do I add a CGPoint to NSMutableArray?

前端 未结 6 1988
小蘑菇
小蘑菇 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: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.

提交回复
热议问题