1

I want to use the openCV2 framework in my iOS app, however, I am more comfortable with swift. I can read and understand obj-c well enough to understand the framework and write the methods I need from it in a bridging header, but I'm not comfortable writing the whole app in obj-C. Unfortunately, there are some data types, (cv::Mat, cv::MserManager, etc) that I will need to use in my datamodel, or possibly elsewhere. Is there a way to include datatypes in my bridging header so that I can work with them in swift?

Gabe Spound
  • 568
  • 5
  • 28

2 Answers2

1

You cannot use C++ types in code called from Swift. However, you can use them in Objective-C++ files, the ones that have the .mm extension. You can mix Objective-C and C++ code in Objective-C++, and can expose Objective-C methods that don't reference C++ in their declarations to Swift via the bridging wrapper. These functions can still use C++ in their implementations, which are not visible in Swift via the bridging header.

You also need to be careful about the language linkage (remember extern "C"?).

Here are some answers that provide examples:

1) Video processing with OpenCV in IOS Swift project

2) Include C++ header file in Swift

3) How to access Swift-objects from a c++ class?

Community
  • 1
  • 1
Anatoli P
  • 4,791
  • 1
  • 18
  • 22
0

Unfortunately, you cannot bridge C++ and Objective-C++ directly into Swift. On the bright side, you can still work with the openCV2 framework in your app, but you'll need to write C or Objective-C wrappers for your types as described in a related question here: Can I mix Swift with C++? Like the Objective - C .mm files

Community
  • 1
  • 1
Ralfonso
  • 1,614
  • 15
  • 30