NSPredicate仅在“年”上过滤。日期字段的一部分 [英] NSPredicate to filter only on "year" part of a date field

查看:114
本文介绍了NSPredicate仅在“年”上过滤。日期字段的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有日期字段的实体,我想选择给定年份的记录。如何构建一个NSPredicate的工作?在Core Data中找不到任何关于日期函数(如果有的话)

I have an entity with a date field and I would like to select the records for a given year. How to build a NSPredicate for the job? Didn't find anything about date functions (if any) in Core Data

感谢

推荐答案

一种可能的方法:

步骤1)请参阅Apple的日期和时间编程指南中的从组件创建日期。创建一个表示年初的NSDate,以及表示年底的NSDate。

Step 1) See "Creating a Date from Components" from Apple's "Date and Time Programming Guide." Make an NSDate representing the beginning of the year, and an NSDate representing the end of the year.

步骤2)然后,您可以构建一个谓词,

Step 2) Then you could build a predicate that searches for objects with date attrs that are greater than the first date and less than the last date.

谓词看起来像这样:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(inceptionDate > %@) AND (inceptionDate < %@)", dateBeginYear, dateEndYear];

这篇关于NSPredicate仅在“年”上过滤。日期字段的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆