Bizarre CoreData error when moving to data model to swift package

When moving my working Core Data model to a swift package, the package/app combo build but the app crashes on launch. The debugger points to the @main attribute on my App struct (using SwiftUI) and prints the following messages in the console:

"CoreData: error:  Failed to load model named StationData

Swift/UnsafeRawBufferPointer.swift:879: Fatal error: UnsafeRawBufferPointer with negative count

2022-03-09 20:46:29.156232-0600 Swift/UnsafeRawBufferPointer.swift:879: Fatal error: UnsafeRawBufferPointer with negative count"

I'm unsure as to what exactly is occurring here, especially since the model built and ran perfectly fine when it was a part of the app instead of the package.

It looks like your trying to load the model from the main bundle. You need to load it from the Swift package bundle and then pass it to the NSPersistentContainer initializer.

Accepted Answer

Changing the module to the swift package instead of the current project module worked for me.

Bizarre CoreData error when moving to data model to swift package
 
 
Q