0

I'm trying to migrate an Objective-C project to Swift which contains a C++ class (say "CPP") and uses OpenCV, which is written in C++. I've created two wrapper classes, "CPPWrapper.h/CPPWrapper.mm" and "OpenCVWrapper.h/OpenCVWrapper.mm" and included the two .h files into the bridging header.

CPPWrapper's interface is like this:

@interface CPPWrapper : NSObject
    @property (nonatomic, readonly) CPP* cpp;
    - (CPPWrapper*) init;
    - (CPPWrapper*) initWithX: (int) X;
    - (bool) takeFoo:(Mat&) foo;
    ......
@end

OpenCVWrapper's interface is like this:

@interface OpenCVWrapper:NSObject <CvVideoCameraDelegate>
    @property (strong, nonatomic) CvVideoCamera *videoCamera;
    @property (assign, nonatomic) cv::Mat X;
    - initWithParentView: (UIImageView *)parentView; 
    - (void) start;
    ......
@end

It seems that the .h files must not contain any C++ component to be bridged successfully with Swift, i.e. the "CPP.h" file and OpenCV can only be included in .mm files. However, to be exposed to Swift, many methods of the wrapper classes having OpenCV- or CPP-type input/output values (like cv::Mat and CPP) and properties of these types have to be defined in the .h files, but the compiler cannot recognised those types without OpenCV libraries and "CPP.h" being included. I am puzzled what to do.

I tried defining the interface in the .mm files, but then they are not accessible to Swift. I have now come up with an idea to use "void" as their type in the interface defined in .h files, and cast to their original type in the implementation so that the compiler won't complain. Is this feasible?

Michael
  • 1
  • 2
  • it's a pain, but... use [this](https://stackoverflow.com/a/48972310/5008845) as reference. It can be done a little better by putting yor C++ stuff in a interface extension in the mm file, but i've no time to show you now.. sorry – Miki Mar 10 '22 at 11:02

0 Answers0