Problems retrieving NSDate object from Core Data using KVC

江枫思渺然 提交于 2019-12-30 12:13:50

问题


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"];

When i use dot notation such as myEntity.updated i get an NSDate object correctly but not when i use a KVC method.

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.


回答1:


You must not call a managed object attribute "updated", that conflicts with the isUpdated method of NSManagedObject.

Similar problems occur if you call and attribute "deleted", compare Core Data NSPredicate "deleted == NO" does not work as expected for a short analysis.

Renaming the attribute should solve your problem. Unfortunately, Xcode does not warn about that.



来源:https://stackoverflow.com/questions/21065705/problems-retrieving-nsdate-object-from-core-data-using-kvc

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