subclassed NSManagedObject does not call description when NSLog'd

混江龙づ霸主 提交于 2020-01-15 06:11:21

问题


I have a data model which has two entities in a relationship of one to many.

Each entity has a class that is subclassed from NSManagedObject.

I get the relation set for the one entity and walk it casting each set member to the specific subclass as I enumerate the collection.

When I do

NSLog(@"My Entity: %@", myEntityInstance);

It logs but does not call my subclass's method for:

- (NSString*) description

It does call if I send:

NSLog(@"My Entity: %@", [myEntityInstance description]);

Any ideas what is being called and why description has to be manually called?

Thanks!


回答1:


I've never seen that. I don't think it's a NSManagedObject behavior. You might log the class before making the call to make sure your instance is of the class you think it is.




回答2:


If a class instance responds to descriptionWithLocale:, then NSLog will use that instead. Although descriptionWithLocale: does not appear in NSManagedObject's list of instance methods, it could possibly still be implemented.

Try overriding descriptionWithLocale: and see if that makes a difference.

- (NSString *) descriptionWithLocale:(id) locale
{
    return @"my description";
}



回答3:


Might be two years or so late to the game, but for the benefit of others, I had this problem tonight. The cause was that, while I had made NSManagedObject subclasses for my entities, one of the entities in the CoreData Modler had it's "Class" set back to NSManagedObject instead of the custom subclass.

It didn't matter what I put into -description in my subclass files, because the objects were coming out of the Context as NSManagedObjects instead of my custom subclass.

Putting the subclass name back in the Entity Inspector in the Xcode Coredata Model Editor fixed it.



来源:https://stackoverflow.com/questions/3936478/subclassed-nsmanagedobject-does-not-call-description-when-nslogd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!