Access C++ Enum from Swift

Viewed 1679

I am trying to access enum's values written in C++ header from Swift. Specifically, I have this enum in OpenCV's hpp header file that I would like to expose its values to Swift. I have attempted to set up a bridging header between Swift and Objective-C, and put a wrapper around C++ enum values I would like to expose, but the compiler isn't happy about it:

imgproc.hpp: The C++ header file

enum ThresholdTypes {
    THRESH_BINARY     = 0,
    THRESH_BINARY_INV = 1,
    THRESH_TRUNC      = 2, 
    ...
};

bridging header:

#import "OpenCVWrapper.h"

OpenCVWrapper.h: My Objective-C Wrapper class to be exposed to Swift

#ifdef __cplusplus
#import <opencv2/core.hpp>
#import <opencv2/imgproc.hpp>
#endif

#import <Foundation/Foundation.h>

@interface OpenCVWrapper : NSObject

typedef enum {
    Binary = cv::THRESH_BINARY,  // ERROR: use of undeclared identifier `cv`
    BinaryInv = cv::THRESH_BINARY_INV  // ERROR: use of undeclared identifier `cv`
} ThresholdingType;

...    

@end

If I move this enum declaration and C++ code (OpenCV) import into OpenCVWrapper.mm then the compiler is ok with it, and I can also use it just fine, but I want to expose this enum to Swift so it has to be in the header file. However, something is not right when I expose the C++ enum directly in Objective-C header.

Is it possible to access C++ constants / enums directly from Objective-C header, in such a way that it can be bridge to Swift?

I have looked at using extern like this and this but the C++ constants are still not recognized in my setup.

2 Answers
Related