Learn Courses My Dashboard

CoreData + Picker: confusion

Hi,

I have two CoreData entities, “Product” and “StorageLocation”; there is a one-to-many relationship between them; “Location” can have multiple product, and product just one location.
Idea behind is:

  • User create a product a choose from a picker the location, then the product is saved and shall be saved the relationship too (location); but the picker gives me back a string, not a “Location” entity which is expected when saving a new product.

Here my code:

@State private var storageLocation = "AAAA"

@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \StorageLocation.name, ascending: true)],
animation: .default)
private var storageLocations: FetchedResults
Form {
Picker(NSLocalizedString(“storage”, comment: “storage”), selection: $storageLocation) {
ForEach(storageLocations, id: .self) { (storageLocation: StorageLocation) in
Text(storageLocation.name!).tag(storageLocation.id)
}