c++ - 如何在 C++ 函数中访问 iOS 项目中的文件?

标签 c++ ios xcode swift opencv

所以我通过我的 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");
}

已添加所有必要的包装器。

enter image description here

最佳答案

我没有找到任何在 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/

相关文章:

带有登录叠加层的 iOS 5 Storyboard(iOS 5.1;Xcode 4.3.3)?

ios - 在 iOS 中自定义 SMS View Controller

c++ - 删除 ListView 中特定项目的复选框

c++ - 模板化类构造函数在结构中使用错误的重载

c++ - 在线 C++ 编译器,我可以在其中禁用复制省略

ios - Dispatch.main.async 节中的行数无效

ios - Swift UITest 在 tableviewcell 中找不到 collectionview

iphone - 在 iPhone App 中集成 ShareKit 2.0

c++ - 是否可以在 C++ 中声明具有不同类型的变量?

android - 本地和远程数据的身份验证的体系结构/实现