1

I have this core data model: (DashboardEntity is parent of Visit and Log) enter image description here

Now, I subclassed DashboardEntity into a class that inherits from NSManagedObject. I also subclassed VisitEntity an LogEntity, both inherit from DashboardEntity.

I want to append an entry to DashboardEntity, but I want this entry to be a VisitEntity, this should be possible as it inherits from DashboardEntity, like this:

                // Fething dashboard entity
                var dashboard = [DashboardEntity]()
                let fetchRequest = NSFetchRequest(entityName: "DashboardEntity")

                // Returning results as an array of DashboardEntities
                do {
                    let results = try managedContext.executeFetchRequest(fetchRequest)
                    dashboard = results as! [DashboardEntity]
                } catch let error as NSError {
                    print("Could not fetch \(error), \(error.userInfo)")
                }


                // Point to DashboardEntity from my database
                let entity =  NSEntityDescription.entityForName("DashboardEntity", inManagedObjectContext:managedContext)

                // Here I create an object of type VisitEntity, that takes an extra parameter that converts json to properties
                let person: VisitEntity = VisitEntity (entity: entity!, insertIntoManagedObjectContext: managedContext, jsonData: index)

                // I append this new object to
                do {
                    try managedContext.save()
                    dashboard.append(person)
                } catch let error as NSError  {
                    print("Could not save \(error), \(error.userInfo)")
                }

But an error occurs :

"Unrecognized selector sent to instance", setManager is the selector (from VisitEntity as it's not available on DashboardEntity).

If I have a normal array of an X object and I want to add an object that inherits from it I can do that in Swift, why it doesn't work with Core Data NSManagedObjects?

It works all fine if I add a DashboardEntity object.

The reason to do this is that I have an UITableViewController that can both contain Visits and Logs, depending of which is it the cell table have some options or others.

Thanks to all!

Mihir Oza
  • 2,768
  • 3
  • 35
  • 61
Vladislav
  • 1,392
  • 1
  • 12
  • 21

1 Answers1

0

It seems to me that the 2 lines here:

 // Point to DashboardEntity from my database
let entity =  NSEntityDescription.entityForName("DashboardEntity", inManagedObjectContext:managedContext)

// Here I create an object of type VisitEntity, that takes an extra parameter that converts json to properties
let person: VisitEntity = VisitEntity (entity: entity!, insertIntoManagedObjectContext: managedContext, jsonData: index)

Create a VisitEntity person instance giving it an entity parameter with the NSEntityDescription being a "DashboardEntity" Instead of a "VisitEntity"

For this reason, when the manager property is trying to be set, the App is saying that DashboardEntity does not have a member "manager".

Maybe try setting the entity description to a VisitEntity:

// Point to DashboardEntity from my database
let entity =  NSEntityDescription.entityForName("VisitEntity", inManagedObjectContext:managedContext)

// Here I create an object of type VisitEntity, that takes an extra parameter that converts json to properties
let person: VisitEntity = VisitEntity (entity: entity!, insertIntoManagedObjectContext: managedContext, jsonData: index)
Prientus
  • 733
  • 6
  • 15
  • Yes, that was it! Thanks! After this had another error and fixed it using this other answer: http://stackoverflow.com/a/26176070/2280527 – Vladislav Nov 05 '15 at 09:15