UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Picker and CoreData

Forums > SwiftUI

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<StorageLocation>
Form {
                Picker(NSLocalizedString("storage", comment: "storage"), selection: $storageLocation) {
                    ForEach(storageLocations, id: \.self) { (storageLocation: StorageLocation) in
Text(storageLocation.name!).tag(storageLocation.id)
            }

How can I get back from the picker a Location object?

Thanks Marco

2      

I digged more into the topic and the problem is not in the picker actually, but in CoreData. What I have is an ActionSheet where I can create a new "Product" entity; in this sheet there is a picker to pick a storage location which is basically another entity. The function addProduct will save then to CoreData

private func addProduct(id: UUID, name: String, actualQty: Int, expDate: Date, location: StorageLocation) {
        withAnimation {
            let newItem = Product(context: viewContext)
            newItem.id = id
            newItem.name = name
            newItem.qty = Int32(actualQty)
            newItem.expDate = expDate
            location.addToProducts(newItem)

After having watched few CoreData videos/tutorials, I understood I have to use the function "addToProducts" provided by the CoreData classes. But when I do this, I get the following error:

Thread 1: "-[StorageLocation addProductsObject:]: unrecognized selector sent to instance 0x600000abcf40"

and the app crashes;

I looked for the error, but everything I found was an incosistency between model and classes, but this is not my case.

Any idea/hints?

Thanks Marco

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.