如何将 CGPoint 添加到 NSMutableArray?

发布于 2024-08-30 05:44:21 字数 418 浏览 11 评论 0原文

我想将我的 CGPoint 存储到 NSMutable 数组中,所以,我有这样的方法:

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

但我收到错误,它说:

参数 1 的类型不兼容 “添加对象”。

所以,我查看了 API,

- (void)addObject:(id)anObject

anObject 添加到末尾的对象 接收者的内容。这个值 不能为零。

所以,我认为“CGPointMake”可以创建一个对象,但不能分配它。会发生什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

没企图 2024-09-06 05:44:22

您还可以执行以下操作:

[myArray addObject:[NSValue valueWithCGPoint:MyCGPoint]];

You can also do the following:

[myArray addObject:[NSValue valueWithCGPoint:MyCGPoint]];
难理解 2024-09-06 05:44:22

不幸的是,CGPoint 不是 Objective-c 对象。它是ac结构。如果你苹果双击 CGPoint 你应该跳转到定义

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

如果你想将 CGPoint 存储在 NSArray 中你需要先包装它们。您可以使用 NSValue 或编写自己的包装器。

请参阅将 CGPoint 转换为 NSValue

编辑>;每个 Objective-C 方法调用都会产生很小的开销,并且创建和销毁对象在用于任何用途之前都会涉及许多方法调用。通常您不必担心这一点,但对于封装很少行为且生命周期较短的非常小的对象,它可能会影响性能。如果苹果使用对象来表示所有点、矩形、大小甚至整数、浮点等,性能会更差。

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.

尤怨 2024-09-06 05:44:22

为了构建 atbreuer11 给出的答案,您可以将 CGPoint 转换为 NSValue,将其存储在 NSMutableArray 中,然后使用以下命令将其转换回来:

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

然后再次恢复它:

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

这可能是最简单的方法。您可以编写一个完整的类并执行所有类型的数据操作,但我认为这有点矫枉过正,除非您真的必须这样做。

编辑

对于 Swift 5(我不确定为什么要这样做,因为我们现在可以使用文字数组,但这里是):

保存值:

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

要检索它:

let points = array.compactMap({ ($0 as? NSValue)?.cgPointValue })

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 })
一张白纸 2024-09-06 05:44:22

斯威夫特3.x
// 将 CGPoint 转换为 NSValue

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

// 再次恢复

var cgPoint : CGPoint!
for i in array {
  cgPoint = i as? CGPoint
}

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
}
疧_╮線 2024-09-06 05:44:22

处理 CGPoint(或任何其他非 NSObject 继承结构)的简单方法是创建一个从 NSObject 继承的新类。

代码较长,但很干净。示例如下:

在 .h 文件中:

@interface MyPoint:NSObject
{
     CGPoint myPoint;   
}

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

@end

在 .m 文件中:

@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

这是一些显示用法的代码示例,不要忘记发布

//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");

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");
动次打次papapa 2024-09-06 05:44:21

问题是 CGPoint 实际上只是一个 C 结构,它不是一个对象:

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

如果您使用的是 iPhone,您可以使用 NSValue UIKit 添加将 CGPoint 转换为 NSValue 对象。

请参阅之前的答案以获取示例:如何我可以简单地将 CGPoint 对象添加到 NSArray 中吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文