Core ML model fails to compile with mixed swift and objective-C

I'm a C++/Python developer recently starting to learn Swift and trying out iOS programming. I have a machine learning model that I've converted into Apple's Core ML format, and have successfully been able to use it in an iOS app in Swift, such as initializing a model with:


var model = MODELNAME()


Now, I have some existing C++ codes that I want to integrate into the app. A simple way to do this is to create a bridging .mm file in Obj-C, and wrapping the C++ code in Obj-c. By itself, the Obj-C wrapper and the C++ codes work without a problem with Swift, and I've been able to use the C++ functions in the iOS Swift app.


However, I noticed a rather confusing bug when I tried to load the Core ML model in the mixed Swift/Objective-C project. If I add .m source codes - even empty files - into the project (which automatically creates a bridging file Project-Bridging-Header.h), the compiler would report an error of "Use of unresolved identifier MODELNAME" for the Core ML model in the Swift code. In an iOS project, adding one .m file is okay, but adding two .m files causes the error to appear. In a Mac console project, on the other hand, simpling adding one .m file will cause the project to fail to compile (well...I understand Core ML is not meant to be running on Mac, but technically speaking it should not have any problem at compile time, so here I'm just doing a minimalistic test to see if the project can go through compilation). Note that, only the .m (and by extension .mm, .cpp) source files affect the compiler, and header files do not.


I'm rather confused with this error...(since somehow, the sheer number of Obj-C sources in the XCode "compiled sources" list - not their contents, affects whether the Core ML model can be compiled). My guess is this might have something to do with how XCode automatically generates a header MODELNAME.h for Core ML models (which usually do not need to be explicitly imported in the code) and this somehow interferes with Obj-C bridging headers Project-Briding-Header.h and the Obj-C source codes.


May I ask if anyone has met similar problems or might have an idea of the reason behind this phenomenon? Thanks!


Screenshot of project files:

https://i.stack.imgur.com/Kk7c1.png

Replies



If you want the swift mlmodel compilation file,

Under build setting->coreml Model Class Genertion Language, select Swift.

command -shift+k Refresh several times


The project you create is an Objective project, and the default configuration is an Objective configuration,

The project that you create is the swift project, and the default configuration is all objective configuration,



What else do you not understand, reply me