objective-cxcodecoremlmlmodel

How to add uncompiled .mlmodel to Xcode UnitTests bundle


I need to add a .mlmodel file to my Unit Tests in order to check programmatic model compilation in my framework. But, since version 12, Xcode automatically compiles any .mlmodel file added to the project and replaces it with .mlmodelc which is unwanted in this context.

So, if I add a TestModel.mlmodel to the project, I can't get a resource URL:

NSBundle *bundle = [NSBundle bundleForClass:ModelTest.class];

// modelURL is nil
NSURL *modelURL = [bundle URLForResource:@"TestModel" 
                           withExtension:@"mlmodel"];

If I open the compiled bundle package I can see TestModel.mlmodelc there.

How can I disable automatic CoreML model compilation in Xcode?


Solution

  • Xcode will try to compile the mlmodel file, no matter in which build phase it is. To copy a non compiled model (which is useful for testing), you can:

    Xcode will then copy CoreML models instead of compiling them. To restore the standard behavior, simply delete the build rule.