当我尝试在iPhone Core-Data应用程序中的列上获取@sum时,我遇到了异常。
我的两个模型正在关注 -
任务模型:
@interface Task : NSManagedObject
{
}
@property (nonatomic, retain) NSString * taskName;
@property (nonatomic, retain) NSSet* completion;
@end
@interface Task (CoreDataGeneratedAccessors)
- (void)addCompletionObject:(NSManagedObject *)value;
- (void)removeCompletionObject:(NSManagedObject *)value;
- (void)addCompletion:(NSSet *)value;
- (void)removeCompletion:(NSSet *)value;
@end
完成模型:
@interface Completion : NSManagedObject
{
}
@property (nonatomic, retain) NSNumber * percentage;
@property (nonatomic, retain) NSDate * time;
@property (nonatomic, retain) Task * task;
@end
这是获取:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"Task" inManagedObjectContext:context];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"taskName" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSError *error;
NSArray *results = [context executeFetchRequest:request error:&error];
NSArray *parents = [results valueForKeyPath:@"taskName"];
NSArray *children = [results valueForKeyPath:@"completion.@sum.percentage"];
NSLog(@"%@ %@", parents, children);
[request release];
[sortDescriptor release];
从底部第四行抛出异常。抛出的异常是:
*** -[NSCFSet decimalValue]: unrecognized selector sent to instance 0x3b25a30
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFSet decimalValue]: unrecognized selector sent to instance 0x3b25a30'
我非常感谢任何帮助。 感谢。
编辑:我在雪豹10.6.3和SDK 3.1.3。
答案 0 :(得分:2)
如果要计算每个任务的总完成百分比总和,可以在任务类中为“completionSum”实现一个getter
// interface (Task.h)
@property (nonatomic, readonly) NSNumber* completionSum;
// implementation (Task.m)
-(NSNumber*) completionSum
{
return [self valueForKeyPath:@"completion.@sum.percentage"];
}
使用@sum计算速度很慢,此解决方案不符合KVO标准。 如果您需要这些,您应该考虑使用KVO实施解决方案。
我已经发布了指向此问题的开源解决方案的链接(http://qr.cx/FVi)