如何将CGPoint添加到NSMutableArray? [英] How do I add a CGPoint to NSMutableArray?
问题描述
我想将我的CGPoint存储到NSMutable数组,所以,我有这样的方法:
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 :
addObject的参数1的不兼容类型。
Incompatible type for argument 1 of "addObject".
所以,我查看了API,
So, I check out the API,
- (void)addObject:(id)anObject
anObject要添加到接收者内容的结尾
的对象。此值
不得为零。
anObject The object to add to the end of the receiver's content. This value must not be nil.
因此,我认为 CGPointMake 可以创建一个Object,但不能分配它。会发生什么?
So, I think the "CGPointMake" can make a Object, but it can't be assigned. What happens?
推荐答案
问题是CGPoint实际上只是一个C结构,它不是一个对象:
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;
如果你在iPhone上,你可以使用NSValue UIKit添加来将CGPoint转换为NSValue对象。
If you are on the iPhone you can use the NSValue UIKit additions to convert the CGPoint to an NSValue object.
有关示例,请参阅此前一个答案:如何以简单的方式将CGPoint对象添加到NSArray?
See this previous answer for examples: How can I add CGPoint objects to an NSArray the easy way?
这篇关于如何将CGPoint添加到NSMutableArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!