使用KVC从Core Data检索NSDate对象时出现问题 [英] Problems retrieving NSDate object from Core Data using KVC
问题描述
我有日期存储在核心数据作为NSDate对象。当我尝试使用获取请求和 - (id)valueForKey:我得到一个整数而不是NSDate对象检索它们。
I have dates stored in core data as NSDate objects. When i try to retrieve them using a fetch request and -(id)valueForKey: i get an integer instead of an NSDate object.
NSError *error = nil;
NSArray *results = [managedObjectContext executeFetchRequest:request error:&error];
NSManagedObject *entity = [results lastObject];
NSDate *date = [entity valueForKey:@"updated"];
当我使用点符号,如myEntity.updated我得到一个NSDate对象正确,但不是当我使用一个KVC方法。
When i use dot notation such as myEntity.updated i get an NSDate object correctly but not when i use a KVC method.
我想使用的原因 - (id)valueForKey:是因为我在核心数据中的每个实体上运行这个代码,我不想以明确地定义每个实体。 Alternativly我可以编写一堆if语句测试isKindOfClass,但是这将需要大量的代码,看起来有点黑客。
The reason I want to use -(id)valueForKey: is because i am running this code on every entity in core data and I don't want to define each entity explicitly. Alternativly i could code a bunch of if statements that test for isKindOfClass, but that will require a lot of code and seems kind of hackish.
任何建议将不胜感激。
Any suggestions would be much appreciated.
推荐答案
不能调用与
You must not call a managed object attribute "updated", that conflicts with the
isUpdated
method of NSManagedObject
.
类似问题发生,如果你调用和属性已删除,
比较核心数据NSPredicate predicateWithBlock和predicateWithFormat在父子上下文中进行简短分析。
Similar problems occur if you call and attribute "deleted", compare Core Data NSPredicate predicateWithBlock and predicateWithFormat in Parent Child Context for a short analysis.
重命名属性应该可以解决您的问题。
不幸的是,Xcode不会警告。
Renaming the attribute should solve your problem. Unfortunately, Xcode does not warn about that.
这篇关于使用KVC从Core Data检索NSDate对象时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!