Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast NSManagedObject to subclassed object

Is there a way to cast an NSManagedObject to a sub-classed object?

I have @interface Contact : NSManagedObject and in a generic part of my code I have an NSManagedObject, I would like to cast it to Contact to be able to access properties directly using contact.firstName etc...

I am using Contact *contact = myManagedObject; which works at run time but I am getting the compiler warning warning: incompatible Objective-C types initializing 'struct NSManagedObject *', expected 'struct Contact *' that I would like to suppress.

like image 837
Chris Wagner Avatar asked Dec 16 '22 19:12

Chris Wagner


1 Answers

Use a C cast:

Contact *contact = (Contact *) myManagedObject;

Be aware that this is quite a bit of rope. Sometimes necessary rope, certainly.

like image 179
bbum Avatar answered Dec 28 '22 08:12

bbum