I have this core data model: (DashboardEntity is parent of Visit and Log)
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!