Std::矢量在objective c方法
std::vector in objective c method
我在objective c++中工作。我遇到的问题是,我需要将std::vector传递给objective - c方法。这可能吗?下面是我当前的代码,我必须在向量定义方法中对向量进行计算(向数组成员添加偏移值),然后将其作为C数组传递给方法。理想情况下,我想在第二种方法中设置偏移量值,从而分割定义(将有几个这样的)。与下面的例子不同,偏移量是可变的。
所以我想使用vectors
而不是传入(b2Vec2 *)vect- (void)createterrain1 {
using namespace std;
vector<b2Vec2>vecVerts;
vector<int>::size_type i;
vecVerts.push_back(b2Vec2(-1022.5f / 100.0, -20.2f / 100.0));
vecVerts.push_back(b2Vec2(-966.6f / 100.0, -18.0f / 100.0));
vecVerts.push_back(b2Vec2(-893.8f / 100.0, -10.3f / 100.0));
vecVerts.push_back(b2Vec2(-888.8f / 100.0, 1.1f / 100.0));
vecVerts.push_back(b2Vec2(-804.0f / 100.0, 10.3f / 100.0));
vecVerts.push_back(b2Vec2(-799.7f / 100.0, 5.3f / 100.0));
vecVerts.push_back(b2Vec2(-795.5f / 100.0, 8.1f / 100.0));
vecVerts.push_back(b2Vec2(-755.2f/ 100.0, -9.5f / 100.0));
vecVerts.push_back(b2Vec2(-632.2f / 100.0, 5.3f / 100.0));
vecVerts.push_back(b2Vec2(-603.9f / 100.0, 17.3f / 100.0));
vecVerts.push_back(b2Vec2(-536.0f / 100.0, 18.0f / 100.0));
vecVerts.push_back(b2Vec2(-518.3f / 100.0, 28.6f / 100.0));
vecVerts.push_back(b2Vec2(-282.1f / 100.0, 13.1f / 100.0));
vecVerts.push_back(b2Vec2(-258.1f / 100.0, 27.2f / 100.0));
vecVerts.push_back(b2Vec2(-135.1f / 100.0, 18.7f / 100.0));
vecVerts.push_back(b2Vec2(9.2f / 100.0, -19.4f / 100.0));
vecVerts.push_back(b2Vec2(483.0f / 100.0, -18.7f / 100.0));
vecVerts.push_back(b2Vec2(578.4f / 100.0, 11.0f / 100.0));
vecVerts.push_back(b2Vec2(733.3f / 100.0, -7.4f / 100.0));
vecVerts.push_back(b2Vec2(827.3f / 100.0, -1.1f / 100.0));
vecVerts.push_back(b2Vec2(1006.9f / 100.0, -20.2f / 100.0));
vecVerts.push_back(b2Vec2(1023.2fdddddd / 100.0, -20.2f / 100.0));
i = vecVerts.size();
//I would like to pass this sets of calculations to the stitch method below rather
than do it here
vector<b2Vec2>::iterator pos;
//add y offset value to our b2Vec2
for(pos = vecVerts.begin();pos != vecVerts.end();++pos)
{
//get b2Vec2 value at index
b2Vec2 currVert = *pos;
//add y offset (this will come from the sprite image size latterly, set value for testing only
b2Vec2 addVert = b2Vec2(currVert.x,currVert.y + 40 /PTM_RATIO);
//copy offset added b2Vec2 back to vector as index
pos->b2Vec2::operator=(addVert);
}
//currently using this as kludge to pass my vector to the stitch method
b2Vec2 * chain = &vecVerts[0];
[self stitchterrainvertswith:chain num:i];
这是我的当前方法传递向量作为一个C风格的数组
-(void)stitchterrainvertswith:(b2Vec2 *)verts num:(int)num {
//create bodydef
b2BodyDef groundBodyDef;
//set body as static
groundBodyDef.type = b2_staticBody;
//set body position
groundBodyDef.position.Set(0, 0);
//create body using def
groundBody = world->CreateBody(&groundBodyDef);
//create shapes
b2EdgeShape screenEdge;
b2ChainShape terrain;
terrain.CreateChain(verts, num);
groundBody->CreateFixture(&terrain,0);
//keeps track of max x value for all the ground pieces that are added to the scene
//maxVerts.x += totalXVerts.x;
}
我尝试使用std::vector的objc包装器,但有点丢失这里是我的例子:
VecWrap.h
#import "Box2D.h"
#include <vector>
struct VecAcc;
@interface VecWrap : NSObject
{
struct VecAcc* vec;
}
@end
VecWrap.MM
#import "VecWrap.h"
struct VecAcc {
std::vector<b2Vec2>data;
};
@implementation VecWrap
-(id)init
{
vec = 0;
if (self == [super init]) {
vec = new VecAcc;
}
return self;
}
-(void)dealloc
{
delete vec;
[super dealloc];
}
@end
,然后创建如下方法:
-(void)stitchgroundvectswith:(VecAcc*)vecs num:(int)num;
哪一个不起作用,这是可能的吗?
都是正确的,Barry Wark的解决方案有效,但我不推荐它,因为像这样的语言特定的预处理器技巧是脆弱的。特别是,如果涉及到名称空间,它们就不能正确工作,并且只能对指针工作。
首先,我强烈建议开发人员尽可能地将他们的ObjC和c++分开,并将objc++最小化到真正需要的几个地方。objc++是一种疯狂的语言,gdb经常遇到问题,影响ARC性能,编译速度较慢,通常是一种有用的黑客,而不是一种真正的语言。
我推荐这种方法来隐藏你的c++方法在头文件中的ObjC:
@interface MyClass
- (void)anOtherMethodCalledFromObjC;
#ifdef __cplusplus
- (void)stitchGroundVectsWithVector:(std::vector<b2Vec2>)vec;
#endif
@end
但一般来说,我建议你的大多数程序是。m和。cpp文件,用一些。mm文件把它们粘合在一起。
有关如何在旧代码中完成此操作的详细讨论,请参见包装c++ -第2例。较新的代码不需要在头文件中添加ivars,所以现在实现起来应该更简单。
您不必编写包装器。这就是objective - c++的目的:你可以声明一个object -C方法来接受/返回一个c++对象,并且它会工作。例如:
- (void)someMethod:(const std::vector<b2Vec2>&)vec {
/* do something with vec in C++ code */
}
对于objective - c++,没有理由不能将stichgroundvectswith:
定义为-(void)stitchgroundvectswith:(std::vector<bVect2>&)vets num:(int)num
,就像你在直接c++中工作一样。在这种模式下,c++对象可以无缝地(有某些警告)集成到Obj-C方法中。
正如@H2CO3和@Joshua Weinberg指出的那样,objective - c++允许你用类型为std::vector
的参数(或其引用等)定义一个方法。如果你在objective - c++中使用包含相关。h的所有文件,这将会很好地工作。然而,如果你需要将头文件导入到Objective-C编译的文件中,你需要传递指针,并在Objective-C代码中隐藏类型:
#ifdef CPLUSPLUS
typedef std::vector* vecp_t
#else
typedef void* vecp_t
#endif
@interface MyClass
{}
- (void)anOtherMethodCalledFromObjC;
- (void)stitchGroundVectsWithVector:(vecp_t)vec;
@end
(我已经采取了自由的Objective-C风格化你的方法名称)
- 从 CPP 代码调用 Objective-C 方法
- C++包装Objective-C的方法:如何为变量添加值
- 在Objective-C中,使用C#Protobuf-Net生成的“序列化Protobuf”的最佳方法是什么?
- 将Objective-C方法传递给C++方法
- 相当于 Objective-C 在 C++ 中的"valueForKey"方法?
- 将objective-c与c++相结合的正确方法
- 通过委托方法让C++模型通知Objective-C++控制器
- 跨平台最佳 MVC 模型到控制器消息传递方法(C#、Objective-C++)
- 在 Objective-C 中调用C++类和方法
- 对于 Xcode 7,是否有等效于 objective-c 的 -debugDescription 方法的C++?
- "Objective C" vs "C++":方法调用语句的差异
- 将Node.js+Socket.io封装到OSX可执行文件中的可靠方法?(或C/C++/Objective-C库作为替代
- 如何使用Objective-C++从Objective-CCocoa接口调用C++方法
- C++虚拟方法引发EXC_BAD_ACCESS(仅当Objective-C++调用时)
- 有效的Objective-C方法在Objective-C++中无效
- Std::矢量在objective c方法
- 在 Objective-C 中调用方法与C++有什么不同
- 消息传递从c++方法转换成Objective-C方法
- 如何在c++中调用Objective-C方法
- 一个c++ signals2槽回调可以包含Objective-C/ c++类/选择器(方法)信息吗?