Let's say I have a CoreData entity called Person
:
class Person: NSManagedObject {
// ...
}
I have been trying to implement a static function create
as an extension of NSManagedObject
.
extension NSManagedObject {
static func create() -> ??? {
let object = ???
CRUD.insertToDatabase(object) // Insert object to database
return object
}
}
Here I want create()
to return an instance of the type from which NSManagedObject
is inherited. For example, when I call create()
from Person
I want it to return a Person
instance. When I call it from the Animal
class it has to return an instance of type Animal
.
let person = Person.create() // -> Returns a Person instance
let animal = Animal.create() // -> Returns an Animal instance
I'd appreciate any help on how to implement this kind of function in the parent class.