0
votes

I have a small iOS application which relies on CoreData. I have at the moment two entities, "Product" and "StorageLocation". There is a one-to-many relationship between them. I generated the classes manually; the Model Editor also shows the correct class name.

When trying to save a new "Product" which contains a relationship to a "StorageLocation" I get this error:

-[StorageLocation addProductsObject:]: unrecognized selector sent to instance 0x60000389d280"

The generated NSManagedObject class is as follows:

import Foundation
import CoreData


extension StorageLocation {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<StorageLocation> {
        return NSFetchRequest<StorageLocation>(entityName: "StorageLocation")
}

@NSManaged public var id: UUID?
@NSManaged public var name: String?
@NSManaged public var products: NSSet?

}

// MARK: Generated accessors for products
extension StorageLocation {

    @objc(addProductsObject:)
    @NSManaged public func addToProducts(_ value: Product)

    @objc(removeProductsObject:)
    @NSManaged public func removeFromProducts(_ value: Product)

    @objc(addProducts:)
    @NSManaged public func addToProducts(_ values: NSSet)

    @objc(removeProducts:)
    @NSManaged public func removeFromProducts(_ values: NSSet)

}

extension StorageLocation : Identifiable {

}

Problem, as error says, is the method "addToProducts" which I invoke like this:

let newItem = Product(context: viewContext)
        newItem.id = id
        newItem.name = name
        newItem.qty = Int32(actualQty)
        newItem.expDate = expDate
        location.addToProducts(newItem)

I also checked if the model matches with the classes, no error found.

Any idea/hint?

Thanks Marco

1
What are the names of the relationships in the core data model between the 2 entities?Joakim Danielson
This is my model: i.imgur.com/ZOtxjz0.pngMarcoGT
Uh? Sorry, I do not see where they do not matchMarcoGT
Have you also the file StoregaLocation+CodeDataClass.swift ?Ptit Xav
What I posted is the file StorageLocation+CoreDataProperties.swiftMarcoGT

1 Answers

0
votes

It's hard to be sure but since you generated the file manually, it might be that it didn't get added to the app target. That would mean the code exists but isn't being compiled as part of the app. Try this:

  1. Click on the file in the file navigator on the left side of the window.
  2. Bring up the "file inspector" tab on the right. That's the one you get if you press cmd-opt-1.
  3. Look in the inspector where it says "target membership". This should list the app plus any test targets, as well as app extensions if you have any.
  4. Make sure your app's name is checked in the target membership list.

If it wasn't checked, and you check it, your code will probably work.