所以我通过我的 OpenCVWrapper
调用我的 C++ 函数。在这个函数中,我必须打开一个位于我的 Xcode 项目中的文件。是否可以在 C++ 函数中指定所需的路径?
或者我必须通过 Swift 中的 OpenCVWrapper
传递文件?
简而言之,它看起来像:
func sample() {
OpenCVWrapper.booProcess()
}
在 OpenCVWrapper.mm
@implementation OpenCVWrapper : NSObject
+ (void)booProcess {
boo();
}
- (void)boo:
{
return boo();
}
@end
闪烁.cpp
void boo()
{
cv::CascadeClassifier my_cascade;
my_cascade.load("bar.xml");
}
已添加所有必要的包装器。
最佳答案
我没有找到任何在 C++ 函数中获取文件路径的正确方法。因此,最明显的解决方案是在 Swift 中获取路径的字符串表示形式。
let path = NSBundle.mainBundle().pathForResource("bar", ofType:"xml")
然后通过包装器将其传递给 C++ 函数
OpenCVWrapper.booProcess(path)
在您的 OpenCVWrapper.mm 中,您必须将 NSString
转换为 std::string
之类的
std::string *mypath = new std::string([path UTF8String]);
然后您可以通过包装器将此路径传递给 C++ 函数。
+ (void)booProcess:(NSString*)path {
std::string *mypath = new std::string([path UTF8String]);
boo(*mypath);
}
关于c++ - 如何在 C++ 函数中访问 iOS 项目中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35664225/