我正在尝试对核心数据实体的日期进行分组和计数。由于时间不同,日期不能分组。所以我试图将自定义函数传递给 nsexpression。但我遇到了一些错误,我不知道正确的方法是什么。
分组和计数代码:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"TEvents"
inManagedObjectContext:context];
NSDictionary *props = [entity propertiesByName];
NSPropertyDescription *propDesc3 = [props objectForKey:@"startDateTime"];
NSExpression *propExpr3 = [NSExpression expressionForKeyPath:@"startDateTime"];
NSExpression *countExpr3 = [NSExpression expressionForFunction:propExpr3 selectorName:@"dateShort" arguments:nil];
NSExpressionDescription *exprDesc3 = [[NSExpressionDescription alloc] init];
[exprDesc3 setExpression:countExpr3];
[exprDesc3 setExpressionResultType:NSDateAttributeType];
[exprDesc3 setName:@"dateStart"];
NSFetchRequest *fr = [NSFetchRequest fetchRequestWithEntityName:@"TEvents"];
[fr setPropertiesToGroupBy:[NSArray arrayWithObjects:propDesc3, nil]];
[fr setPropertiesToFetch:[NSArray arrayWithObjects:propDesc3, exprDesc3, nil]];
[fr setResultType:NSDictionaryResultType];
NSError * error = nil;
NSArray *results = [context executeFetchRequest:fr error:&error];
dateShort 是一个 nsdate 类别函数,它将数据时间转换为用户的当前区域:
-(NSString *) dateShort
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setCalendar:[NSCalendar currentCalendar]];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
[formatter setDoesRelativeDateFormatting:YES];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
NSString *tmpValue = [formatter stringFromDate:self];
return tmpValue;
}
错误:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“传递给 SQL 存储的函数类型不受支持”
如何实现这一目标:将日期时间转换为当前区域和组并依靠核心数据实体上的 (yy-mm-dd)?