如何将 CGPoint 添加到 NSMutableArray?
我想将我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您还可以执行以下操作:
You can also do the following:
不幸的是,CGPoint 不是 Objective-c 对象。它是ac结构。如果你苹果双击 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
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.
为了构建 atbreuer11 给出的答案,您可以将 CGPoint 转换为 NSValue,将其存储在 NSMutableArray 中,然后使用以下命令将其转换回来:
然后再次恢复它:
这可能是最简单的方法。您可以编写一个完整的类并执行所有类型的数据操作,但我认为这有点矫枉过正,除非您真的必须这样做。
编辑
对于 Swift 5(我不确定为什么要这样做,因为我们现在可以使用文字数组,但这里是):
保存值:
要检索它:
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:
Then restore it again:
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:
To retrieve it:
斯威夫特3.x
// 将 CGPoint 转换为 NSValue
// 再次恢复
Swift 3.x
// Convert CGPoint to NSValue
// Restore it again
处理 CGPoint(或任何其他非 NSObject 继承结构)的简单方法是创建一个从 NSObject 继承的新类。
代码较长,但很干净。示例如下:
在 .h 文件中:
在 .m 文件中:
这是一些显示用法的代码示例,不要忘记发布!
A simple way to handle
CGPoint
(or any other nonNSObject
inherited structure) is to create a new class inherited fromNSObject
.The code is longer, but clean. An example is below:
In .h file:
In .m file:
Here is some code sample showing the usage, do not forget to release!!!
问题是 CGPoint 实际上只是一个 C 结构,它不是一个对象:
如果您使用的是 iPhone,您可以使用 NSValue UIKit 添加将 CGPoint 转换为 NSValue 对象。
请参阅之前的答案以获取示例:如何我可以简单地将 CGPoint 对象添加到 NSArray 中吗?
The problem is that CGPoint is actually just a C structure it is not an object:
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?