无法转换“b2PolygonShape”到“objc_object*”在参数传递中
我不确定你们中的许多人是否熟悉 box2d 物理引擎,但我在 cocos2d 和 Objective C 中使用它。
不过,这或多或少可能是一个通用的 Objective-C 问题,我正在执行以下操作:
NSMutableArray *allShapes = [[NSMutableArray array] retain];
b2PolygonShape shape;
..
..
[allShapes addObject:shape];
并在构建时的 addObject 定义上收到此错误:
cannot convert 'b2PolygonShape' to 'objc_object*' in argument passing
所以或多或少我想我想知道如何将 b2PolygonShape 添加到可变数组。 b2PolygonShape 似乎只是一个类,而不是一个结构或类似的东西。我在谷歌上找到的最接近的东西我认为可以做到这一点被描述为“将 b2PolygonShape 封装为 NSObject,然后将其添加到数组中”,但不确定执行此操作的最佳方法,但我会认为这是对象应该使用 addObject
添加,因为我的一些其他实例化类对象可以很好地添加到数组中。
这都是因为 b2PolygonShape 没有在其根继承 NSObject
吗?
谢谢
I am not sure if many of you are familiar with the box2d physics engine, but I am using it within cocos2d and objective c.
This more or less could be a general objective-c question though, I am performing this:
NSMutableArray *allShapes = [[NSMutableArray array] retain];
b2PolygonShape shape;
..
..
[allShapes addObject:shape];
and receiving this error on the addObject definition on build:
cannot convert 'b2PolygonShape' to 'objc_object*' in argument passing
So more or less I guess I want to know how to add a b2PolygonShape to a mutable array. b2PolygonShape appears to just be a class, not a struct or anything like that. The closest thing I could find on google to which I think could do this is described as 'encapsulating the b2PolygonShape as an NSObject and then add that to the array', but not sure the best way to do this, however I would have thought this object should add using addObject
, as some of my other instantiated class objects add to arrays fine.
Is this all because b2PolygonShape does not inherit NSObject
at it's root?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
b2PolygonShape
是一个 C++ 类,而不是 ObjC 类。您只能将 ObjC 实例放入“NS-containers”中。由于无论如何您都需要 C++,所以最好使用
std::vector
。b2PolygonShape
is a C++ class, not an ObjC class. You can only put ObjC instances into "NS-containers".Since you need C++ anyway, it's better to use a
std::vector<b2PolygonShape>
.NS 容器类(正如 KennyTM 指出的那样)只能存储 NSObject。有时这可能会有点痛苦。但是 NS 容器有很多替代品。
您可以编写 Objective-C 包装类(或使用 NSValue),并将它们存储在 NSArray 中。
您可以使用普通的旧 C 数组(但是,如果数组大小未定义并且会缩小和增长,这可能无法满足您的需求)
您可以使用哈希表来存储引用。
结构体的链接列表也可以派上用场,并且相当容易创建和维护。
如果您决定坚持使用 std::vector,这是一个很好的解决方案,您可以在以下位置阅读更多相关信息: http://www.cplusplus.com/reference/stl/vector/
NS-container classes can (as KennyTM pointed out) only store NSObjects. This can be a bit of a pain sometimes. But there are plenty of alternatives to NS-containers.
You can write Objective-C wrapper classes (or use NSValue), and store these in an NSArray.
You could use a plain old C array (though, that may not serve your needs, if the array size is undefined and shrinks and grows)
You could use a hash table to store your references.
A linked list of structs can also come in handy, and is fairly easy to create and maintain.
Should you decide to stick to std::vector, which is as good a solution as any, you can read more about that at: http://www.cplusplus.com/reference/stl/vector/