大佬教程收集整理的这篇文章主要介绍了iOS:NSPredicate使用NSDate进行比较,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
426174354 = 2014年7月4日
我需要使用谓词创建(3)NSFetchrequest.
对于startDate:
> fetchrequest1使用谓词需要根据用户的设备时间获取今天日期中的所有内容.
使用谓词的fetchrequest2需要根据用户的设备时间来获取过去的所有内容,这意味着昨天和之前的内容.
使用谓词的fetchrequest3需要获取将来的所有内容,这意味着基于用户设备时间的明天和之后的开始.
以下是我到目前为止的代码:
-(NSMutableArray *)getFetchPreDicate:(NSUInteger)fetchrequestType { NSDate *Now = [self getcurrentTime:[NSDate date]]; NSDateFormatter *format = [[NSDateFormatter alloc] init]; format.dateFormat = @"dd-MM-yyyy"; format.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]; NSString *StringDate = [format StringFromDate:Now]; NSDate *todaysDate = [format dateFromString:stringDate]; //today's date is Now a date without time. NSMutableArray *subpreDicates; if (fetchrequestType == 1) { NSPreDicate *subPredToday = [NSPreDicate preDicateWithFormat:@"startDate == %@ ",todaysDate]; [subpreDicates addObject:subPredToday]; } else if (fetchrequestType == 2) { NSPreDicate *subPredPast = [NSPreDicate preDicateWithFormat:@"startDate < %@",todaysDate]; [subpreDicates addObject:subPredPast]; } else if (fetchrequestType == 3) { NSPreDicate *subPredFuture = [NSPreDicate preDicateWithFormat:@"startDate > %@",todaysDate]; [subpreDicates addObject:subPredFuture]; } return subPreDicates; } -(NSDate *)getcurrentTime:(NSDate*)date { NSDate *@R_489_9016@eDate = date; NSTimeZone* @R_489_9016@eTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; NSTimeZone* desTinationTimeZone = [NSTimeZone systemTimeZone]; NSInteger @R_489_9016@eGMTOffset = [@R_489_9016@eTimeZone secondsFromGMTForDate:@R_489_9016@eDate]; NSInteger desTinationGMTOffset = [desTinationTimeZone secondsFromGMTForDate:@R_489_9016@eDate]; NSTimeInterval interval = desTinationGMTOffset - @R_489_9016@eGMTOffset; NSDate* deviceDateWithTime = [[NSDate alloc] initWithTimeInterval:interval sinceDate:@R_489_9016@eDate]; return deviceDateWithTime; }
上面的代码没有从CoreData获取正确的对象.我有一种感觉我的谓词比较是不正确的.我不知道如何将startDate中的存储时间转换为仅日期格式,并将其应用于PreDicates进行比较.有什么建议么?
我想你必须创建两个不同的NSDate对象:startOfCurrentDay(例如00:00:00)和endOfCurrentDay(例如23:59:59),并且亲自地使用NSCalendar.如果这样做,您的提取请求谓词将是:
if (fetchrequestType == 1) { NSPreDicate *subPredToday = [NSPreDicate preDicateWithFormat:@"(startDate >= %@) AND (startDate <= %@)",startOfCurrentDay,endOfCurrentDay]; } else if (fetchrequestType == 2) { NSPreDicate *subPredPast = [NSPreDicate preDicateWithFormat:@"startDate < %@",startOfCurrentDay]; } else if (fetchrequestType == 3) { NSPreDicate *subPredFuture = [NSPreDicate preDicateWithFormat:@"startDate > %@",endOfCurrentDay]; }
以上是大佬教程为你收集整理的iOS:NSPredicate使用NSDate进行比较全部内容,希望文章能够帮你解决iOS:NSPredicate使用NSDate进行比较所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。