我有一个具有日期字段的实体,我想选择给定年份的记录。如何构建一个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仅在“年”上过滤。日期字段的一部分
发布评论