0

So I call my C++ function through my OpenCVWrapper. In this function I have to open a file which is located in my Xcode project. Is it possible to specify the desired path in C++ function? Or I have to pass a file through the OpenCVWrapper in Swift?

In short, it looks smth like:

func sample() {
    OpenCVWrapper.booProcess()
}

in OpenCVWrapper.mm

@implementation OpenCVWrapper : NSObject

+ (void)booProcess {
    boo();
}

- (void)boo:
{
    return boo();
}

@end

blink.cpp

void boo()
{
    cv::CascadeClassifier my_cascade;
    my_cascade.load("bar.xml");
}

All necessary wrappers have been added.

enter image description here

albertpod
  • 155
  • 2
  • 11

2 Answers2

4

I didn't find any proper way to get a file path in C++ function. So the most obvious solution is getting a String representation of the path in Swift.

let path = NSBundle.mainBundle().pathForResource("bar", ofType:"xml")

Then pass it through the wrapper to C++ function

OpenCVWrapper.booProcess(path)

In your OpenCVWrapper.mm you have to convert a NSString into a std::string like

std::string *mypath = new std::string([path UTF8String]);

Then you may pass this path to C++ function through the wrapper.

+ (void)booProcess:(NSString*)path {
    std::string *mypath = new std::string([path UTF8String]);
    boo(*mypath);
}
albertpod
  • 155
  • 2
  • 11
0

Unfortunately it is not currently possible to call C++ code from Swift. You will have to wrap OpenCVWrapper in Objective-C, and then call it in Swift.

See Using Swift with Objective-C for more info.

Alex Popov
  • 2,509
  • 1
  • 19
  • 30
  • Eh? I've made all necessary wrappers for C++ code.. The question is not about directly using C++ from swift. It's about 'how to access files in iOS project inside C++ function?' – albertpod Feb 27 '16 at 01:11
  • Take a look at this: http://stackoverflow.com/questions/23438393/new-to-xcode-cant-open-files-in-c If this is it, I'll just change my answer to link to it and give a brief synopsis. – Alex Popov Feb 27 '16 at 01:14
  • Actually, there is a sense to check, but I am not sure that opening file in command line-like project is equivalent to the opening file in iOS. – albertpod Feb 27 '16 at 01:33
  • I've watched. It has nothing common with my issue. – albertpod Feb 27 '16 at 18:42