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.
Use a C cast:
Contact *contact = (Contact *) myManagedObject;
Be aware that this is quite a bit of rope. Sometimes necessary rope, certainly.
精彩评论