如何获得 Core Data 列的平均值?

发布于 2024-09-28 01:56:14 字数 273 浏览 2 评论 0原文

如果我有以下 NSManagedObject,如何获得 number1 值的平均值和 number2 值的平均值?

@interface Log :  NSManagedObject  
{


}


@property (nonatomic, retain) NSNumber * number1;
@property (nonatomic, retain) NSNumber * number2;

谢谢:D

If I have the following NSManagedObject, how can I get the average of values for number1 and the average of values for number2?

@interface Log :  NSManagedObject  
{


}


@property (nonatomic, retain) NSNumber * number1;
@property (nonatomic, retain) NSNumber * number2;

Thanks :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

当爱已成负担 2024-10-05 01:56:14
 NSManagedObjectContext *managedObjectContext = [(AppDelegate_Shared*)[UIApplication sharedApplication].delegate managedObjectContext];

 NSFetchRequest *request = [[NSFetchRequest alloc] init];
 NSEntityDescription *entity = [NSEntityDescription entityForName:@"Log" inManagedObjectContext:managedObjectContext];
 [request setEntity:entity];

 // Specify that the request should return dictionaries.
 [request setResultType:NSDictionaryResultType];

 // Create an expression for the key path.
 NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"systolic"];

 // Create an expression to represent the minimum value at the key path 'creationDate'
 NSExpression *avgExpression = [NSExpression expressionForFunction:@"average:" arguments:[NSArray arrayWithObject:keyPathExpression]];

 // Create an expression description using the minExpression and returning a date.
 NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];

 // The name is the key that will be used in the dictionary for the return value.
 [expressionDescription setName:@"averageSystolicPressure"];
 [expressionDescription setExpression:avgExpression];
 [expressionDescription setExpressionResultType:NSInteger32AttributeType];

 // Set the request's properties to fetch just the property represented by the expressions.
 [request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];

 // Execute the fetch.
 NSError *error;
 NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error];
 if (objects == nil) {
  // Handle the error.
 }
 else {
  if ([objects count] > 0) {
   NSLog(@"object count = %d", [objects count]);

   NSLog(@"Average systolic pressure: %d", [[[objects objectAtIndex:0] valueForKey:@"averageSystolicPressure"] integerValue] );
  }
 }

 [expressionDescription release];
 [request release];
 NSManagedObjectContext *managedObjectContext = [(AppDelegate_Shared*)[UIApplication sharedApplication].delegate managedObjectContext];

 NSFetchRequest *request = [[NSFetchRequest alloc] init];
 NSEntityDescription *entity = [NSEntityDescription entityForName:@"Log" inManagedObjectContext:managedObjectContext];
 [request setEntity:entity];

 // Specify that the request should return dictionaries.
 [request setResultType:NSDictionaryResultType];

 // Create an expression for the key path.
 NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"systolic"];

 // Create an expression to represent the minimum value at the key path 'creationDate'
 NSExpression *avgExpression = [NSExpression expressionForFunction:@"average:" arguments:[NSArray arrayWithObject:keyPathExpression]];

 // Create an expression description using the minExpression and returning a date.
 NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];

 // The name is the key that will be used in the dictionary for the return value.
 [expressionDescription setName:@"averageSystolicPressure"];
 [expressionDescription setExpression:avgExpression];
 [expressionDescription setExpressionResultType:NSInteger32AttributeType];

 // Set the request's properties to fetch just the property represented by the expressions.
 [request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];

 // Execute the fetch.
 NSError *error;
 NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error];
 if (objects == nil) {
  // Handle the error.
 }
 else {
  if ([objects count] > 0) {
   NSLog(@"object count = %d", [objects count]);

   NSLog(@"Average systolic pressure: %d", [[[objects objectAtIndex:0] valueForKey:@"averageSystolicPressure"] integerValue] );
  }
 }

 [expressionDescription release];
 [request release];
讽刺将军 2024-10-05 01:56:14

使用集合运算符@avg

假设您已经提取了Log对象并将生成的NSSet存储在中日志。然后您可以简单地说:

NSNumber *avg1 = [logs valueForKeyPath:@"@avg.number1"];
NSNumber *avg2 = [logs valueForKeyPath:@"@avg.number2"];

@avg 是可与关键路径中的集合一起使用的少数运算符之一。其他一些是 @max@min@sum

Use the collection operator @avg.

Let's say you've already done a fetch for your Log objects and stored the resulting NSSet in logs. Then you can simply say:

NSNumber *avg1 = [logs valueForKeyPath:@"@avg.number1"];
NSNumber *avg2 = [logs valueForKeyPath:@"@avg.number2"];

@avg is one of a small handful of operators that you can use with collections in key paths. A few others are @max, @min, and @sum.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文