按日期(yyyy / MM / dd)与NSFetchRequest组合

编程入门 行业动态 更新时间:2024-10-27 10:21:59
本文介绍了按日期(yyyy / MM / dd)与NSFetchRequest组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我必须计数和分组更多的3个属性。 我正在拉我的头发,而分组和计数字段与日期(yyyy / mm / dd HH:mm:ss)我想按年 - 月 - 日分组,忽略时间。但我仍然需要时间的细节视图。时区在这里起着很大的作用。所以我只是保存它的utc格式。

i have to count and group on more that 3 properties. i'm pulling my hair while grouping and counting on field with date (yyyy/mm/dd HH:mm:ss) i want to group by year - month - day and ignore the time. but i still need the time for detail view. time zone plays a big role here. so i just save it in utc format.

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?

how can i can group by date (yyyy/MM/dd)? and be ok for converting to UTC?

提前感谢

ps:工作和无法获取。

ps: group by on a transient property doesnt work and cant be fetched.

推荐答案

我不得不转换日期的本地时区与fecth结果控制器如下

i had to convert date on the fly for local timezone with the fecth result controller as follow

[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"];

然后手动对它们进行分组,并且表视图现在使用数组而不是fetch result controller。

then manually group them and table view now use an array instead of fetch result controller.

更多推荐

按日期(yyyy / MM / dd)与NSFetchRequest组合

本文发布于:2023-11-22 08:39:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616725.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   按日   yyyy   MM   dd

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!