NSManagedObject 子类和 setValuesForKeysWithDictionary:
我正在初始化一个 NSManagedObject
子类,使用:
- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues
我还故意给它未定义的键,这当然应该抛出异常。
因此,我实现了:
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
如果某个键未定义,我将其映射到正确的键。
但是,我的实现永远不会被调用,而是使用 NSManagedObject
的实现,这又仍然抛出异常。
是否存在阻止 setValuesForKeysWithDictionary:
使用 setValue:forUndefinedKey:
的任何 NSManagedObject
子类实现的行为(或错误)?
按要求添加代码,但由于我只是实现一个现有方法,该方法不会被调用,所以我不知道它会有多大帮助:
在上下文中:
- (Company*)companyWithDictionary:(NSDictionary*)values{
Company* newCompany = (Company*)[NSEntityDescription insertNewObjectForEntityForName:@"Company" inManagedObjectContext:self];
[newCompany setValuesForKeysWithDictionary:values];
return newCompany;
}
在 Company
子类中(从未打电话过):
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
if([key isEqualToString:idKey]){
[self setValue:value forKey:@"uniqueID"];
}else if([key isEqualToString:descriptionKey]){
[self setValue:value forKey:@"descriptionText"];
}else if([key isEqualToString:timeUpdatedKey]){
[self setValue:value forKey:@"dateUpdated"];
}else{
[super setValue:value forUndefinedKey:key];
}
}
I am initializing a NSManagedObject
subclass using:
- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues
I am also knowingly giving it the undefined keys, which of course should throw an exception.
So, I have implemented:
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
If a key is undefined, I map it to the correct key.
However, my implementation never gets called, instead NSManagedObject
's implementation is used, which in turn still throws an exception.
Is there a behavior (or bug) that prevents setValuesForKeysWithDictionary:
from using any NSManagedObject
subclass implementation of setValue:forUndefinedKey:
?
Adding code as requested, but since I am just implementing an existing method, which doesn't get called, I don't know how much help it will be:
In the context:
- (Company*)companyWithDictionary:(NSDictionary*)values{
Company* newCompany = (Company*)[NSEntityDescription insertNewObjectForEntityForName:@"Company" inManagedObjectContext:self];
[newCompany setValuesForKeysWithDictionary:values];
return newCompany;
}
In the Company
subclass (never called):
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
if([key isEqualToString:idKey]){
[self setValue:value forKey:@"uniqueID"];
}else if([key isEqualToString:descriptionKey]){
[self setValue:value forKey:@"descriptionText"];
}else if([key isEqualToString:timeUpdatedKey]){
[self setValue:value forKey:@"dateUpdated"];
}else{
[super setValue:value forUndefinedKey:key];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NSManagedObject 绝对没有任何内在的东西可以阻止这种情况;我正在我正在开发的应用程序中执行此操作。你能发布更多你的代码吗?
还有一个想法:您确定是
setValue:forUndefinedKey:
引发了异常吗?对于托管对象,我很确定您还需要实现valueForUndefinedKey:
(getter)才能使事情正常工作。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 implementvalueForUndefinedKey:
(the getter) to make things work properly.简单的问题,但是您是否为模型中的实体指定 NSManagedObject 子类的类名?
另外,你说你正在初始化托管对象......如何初始化?在大多数情况下,您应该通过 +[NSEntityDescription insertNewObjectForEntityForName: inManagedObjectContext:] 插入它,除非您有充分的理由不这样做。
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.
CoreData 框架的快速类转储表明 NSManagedObject 还重写了 setValuesForKeysWithDictionary: ,因此看起来它肯定正在对该方法进行一些自定义,可能是出于性能原因或类似的事情。我可能会编写自己的与 setValuesForKeysWithDictionary: 等效的方法并调用它以避免整个混乱。像这样的东西:
A quick class-dump of the CoreData framework indicates that
NSManagedObject
also overridessetValuesForKeysWithDictionary:
, 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 tosetValuesForKeysWithDictionary:
and call that instead to avoid the whole mess. Something like this: