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.
Use a C cast:
Contact *contact = (Contact *) myManagedObject;
Be aware that this is quite a bit of rope. Sometimes necessary rope, certainly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With