Core ML in Swift Playground

I wanted to create an App that utilizes the Vision Framework for the 2023 Swift Student Challenge. From the few post regarding the topic I tried to create a dummy project and build it to create a mlmodelc-file that I copied into the recourses folder and copied the automatically generated swift code into the source folder. Somehow the playground crashes when trying to force-unwrap the URL, which means it cannot find the model file (I also tried the same with an mlmodel file).

class var urlOfModelInThisBundle: URL {
    let resPath = Bundle(for: self).url(forResource: "FoodClassifier", withExtension: "mlmodel") /* Here it fails already */
    return try! MLModel.compileModel(at: resPath)
 }

The naming is accurate and the dummy app is working. Is there something that I am not seeing?

It might be worth adding that it seems like using Bundle.main.url (or Bundle(for: self)) always fails. For some reason ModelEntity.load(named: "apple.usdz") works while Bundle.main.url(forResource: "apple", withExtension: "usdz") doesn´t. All the tutorials say the latter is the right way to load resources, but has Apple changed something?

I am building something with CoreML with exact same technique you are using, and it works for me. I think what's happening with your project is the model file isn't being copied to your app bundle. Because playground didn't properly assign file's target membership. In my case, my project works and compile fine with the playgrounds app (macOS & iPadOS). But would crash if I open my project within Xcode. Because playground project current't doesn't support target at all. I can't find anyway to fix this. Except I would suggest reimport the model with the playground app.

Hey jakedves, I am also facing the same problem, please help me with it, I tried your solution yet facing the same proble.

Hi , I am facing the same issue too, anyone have a solution ?

Core ML in Swift Playground
 
 
Q