2010-05-31 123 views
0

當我嘗試在iPhone Core-Data應用程序的列上獲取@sum時,發生異常。核心數據@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:@"[email protected]"]; 
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上。

回答

2

如果你要計算每個任務的總完工百分比總和,你可以實現一個getter方法「completionSum」在任務類

// interface (Task.h) 
@property (nonatomic, readonly) NSNumber* completionSum;  

// implementation (Task.m) 
-(NSNumber*) completionSum 
{ 
    return [self valueForKeyPath:@"[email protected]"]; 
} 

使用@sum是緩慢的計算,該解決方案不符合志願。 如果您需要這些,您應該考慮使用KVO來實施解決方案。

我已經發布了一個指向此問題的開源解決方案的鏈接(http://qr.cx/FVi