0

我必须对超过 3 个属性进行计数和分组。我在分组并计算日期字段时拉头发(yyyy / mm / dd HH:mm:ss)我想按年-月-日分组并忽略时间。但我仍然需要时间查看详细信息。时区在这里起着重要作用。所以我只是以utc格式保存它。

NSFetchRequest *fetchRequest    = [[NSFetchRequest alloc] init];
NSEntityDescription *entity     = [NSEntityDescription entityForName:@"logs"
                                              inManagedObjectContext:context;
NSAttributeDescription* att3 = [entity.attributesByName objectForKey:@"savedDate"];
NSExpression *keyPathExpression3 = [NSExpression expressionForKeyPath: @"savedDate"];
NSExpression *countExpression3 = [NSExpression expressionForFunction: @"count:"
                                                           arguments: [NSArray arrayWithObject:keyPathExpression3]];
NSExpressionDescription *att3Description = [[NSExpressionDescription alloc] init];
[expressionDescription3 setName: @"countSavedDate"];
[expressionDescription3 setExpression: countExpression3];
[expressionDescription3 setExpressionResultType: NSInteger32AttributeType];

...

[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:att1, att1Description, att2, att2Description, att3, att3Description, att4Description, att4, nil]];
[fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObjects:att1, att2, att2, att3, nil]];
[fetchRequest setResultType:NSDictionaryResultType];

我怎样才能按日期分组(yyyy/MM/dd)?可以转换为 UTC 吗?

提前致谢

ps:对瞬态属性进行分组不起作用且无法获取。

4

1 回答 1

0

我必须使用 fecth 结果控制器即时转换本地时区的日期,如下所示

[self willAccessValueForKey:@"startDateTime"];

NSDate * date = [self valueForKey:@"startDateTime"];

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:date];
[self didAccessValueForKey:@"callStartDateTime"];

然后手动对它们进行分组,表视图现在使用数组而不是获取结果控制器。

于 2013-06-23T15:29:21.580 回答