Piotr Piotr - 5 months ago 31x
Swift Question

How to insert object with abstract parent in Core Data iOS Swift

I create two entity in core data. First is Car(Abstract) with some attributes and second is SportCar(parent entity is Car) .

I generate swift classes from model in Xcode. Now I want to insert object of SportCar into database in code.

I don't know how do this correct way. Can anybody help me?

let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

if let sportsCar = NSEntityDescription.insertNewObjectForEntityForName("SportsCar", inManagedObjectContext: context) as? SportsCar {

sportsCar.maxSpeed = 20


I guess that I have to add some code to sportCar class that enable me to get car attributes. Can anybody write my how to do it in convenient way?


You can access the properties directly, either from the subclass:

self.carName = "Betty"

Or from outside:

sportsCar.carName = "Betty"