开发者

Cast NSManagedObject to subclassed object

开发者 https://www.devze.com 2023-01-29 07:27 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消