How to Add .mlmodel File to Xcode App Playgrounds (.swiftpm) Project? - Stack Overflow

admin2025-04-30  0

I’m working on an Xcode App Playground project (.swiftpm) and trying to add a .mlmodel file (e.g., Exercises.mlmodel) to it. However, when I add the .mlmodel file to my project, I encounter the following error:

Exercises.mlmodel: No predominant language detected. Set COREML_CODEGEN_LANGUAGE to preferred language.

The .mlmodel file works perfectly fine when used in a regular Xcode project (.xcodeproj), but this issue occurs as soon as I add the file in an App Playground project (.swiftpm).

Steps I’ve tried:

  1. Ensuring the .mlmodel file is correctly added to the project folder.
  2. Checking the generated Core ML Swift code in a .xcodeproj environment—works as expected.
  3. Searching for a way to explicitly set COREML_CODEGEN_LANGUAGE in a .swiftpm project, but it seems that Xcode does not provide this option.

I have tried the solutions in these articles, however none of them worked:

/@sofiadinizms/how-to-use-coreml-in-swift-playgrounds-8d5f001c5d15

#776359022

I’m working on an Xcode App Playground project (.swiftpm) and trying to add a .mlmodel file (e.g., Exercises.mlmodel) to it. However, when I add the .mlmodel file to my project, I encounter the following error:

Exercises.mlmodel: No predominant language detected. Set COREML_CODEGEN_LANGUAGE to preferred language.

The .mlmodel file works perfectly fine when used in a regular Xcode project (.xcodeproj), but this issue occurs as soon as I add the file in an App Playground project (.swiftpm).

Steps I’ve tried:

  1. Ensuring the .mlmodel file is correctly added to the project folder.
  2. Checking the generated Core ML Swift code in a .xcodeproj environment—works as expected.
  3. Searching for a way to explicitly set COREML_CODEGEN_LANGUAGE in a .swiftpm project, but it seems that Xcode does not provide this option.

I have tried the solutions in these articles, however none of them worked:

https://medium.com/@sofiadinizms/how-to-use-coreml-in-swift-playgrounds-8d5f001c5d15

https://developer.apple.com/forums/thread/743942?answerId=776359022#776359022

Share Improve this question edited Jan 26 at 17:06 HangarRash 15.1k5 gold badges21 silver badges55 bronze badges asked Jan 4 at 23:43 MohAliBouMohAliBou 1111 silver badge10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The developer thread you linked actually had the answer. In short,

  1. Go into your packages.swift file.

  2. Change your iOS version to 18.2 or whatever is the latest.

  3. Make your targets look like this.

I had the same problem, for the Swift student challenge, here's a snippet of the code I changed:

let package = Package(
    name: "SSC Project",
    platforms: [
        .iOS("18.2")
    ], targets: [
        .executableTarget(
            name: "AppModule",
            path: ".",
            resources: [
                .copy("Exercises")
            ]
        )
    ]
)

Where you see exercises, that's the folder I kept my ML model in, so change that to whatever folder you have it in, and change iOS version too. That's it.

转载请注明原文地址:http://anycun.com/QandA/1746024023a91492.html