NSManagedObject subclasses and setValuesForKeysWithDictionary:

帅比萌擦擦* 提交于 2019-12-05 05:36:29

There's definitely nothing intrinsic to NSManagedObject that prevents this; I'm doing exactly this in the app I'm working on. Can you post more of your code?

One thought, also: are you sure it's setValue:forUndefinedKey: that's throwing the exception? For a managed object, I'm pretty sure you also need to implement valueForUndefinedKey: (the getter) to make things work properly.

Simple question, but are you specifying your NSManagedObject subclass's class name for the entity in your model?

Also, you said you're initializing the managed object ... how? You should be inserting it via +[NSEntityDescription insertNewObjectForEntityForName: inManagedObjectContext:] in most cases, unless you have a good reason not to.

A quick class-dump of the CoreData framework indicates that NSManagedObject also overrides setValuesForKeysWithDictionary:, so it looks like it definitely is doing some customization for that method, perhaps for performance reasons or something like that. I would probably just write my own method equivalent to setValuesForKeysWithDictionary: and call that instead to avoid the whole mess. Something like this:

- (void)mySetValuesForKeysWithDictionary:(NSDictionary*)dict
{
    for (NSString* key in dict)
        [self setValue:[dict objectForKey:key] forKey:key];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!