iphone - NSPredicate 仅过滤日期字段的 "year"部分

标签 iphone ios core-data date nspredicate

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

谢谢

最佳答案

最终我或多或少按照 Wienke 的建议做了。要创建获取特定年份记录的谓词,我是这样做的:

- (NSPredicate*) predicateFromYear:(NSInteger)start span:(NSInteger)aSpan {
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *dc = [NSDateComponents new];

[dc setYear:start];

NSDate *startDate,*endDate;

startDate = [cal dateFromComponents:dc];
[dc setYear:aSpan];
endDate = [cal
           dateByAddingComponents:dc
           toDate:startDate
           options:0];

[dc release];
return [NSPredicate predicateWithFormat:
        @"date >= CAST(%f, \"NSDate\") AND date < CAST(%f, \"NSDate\")",
        [startDate timeIntervalSinceReferenceDate],
        [endDate timeIntervalSinceReferenceDate]];  }

关于iphone - NSPredicate 仅过滤日期字段的 "year"部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5365258/

相关文章:

ios - MKMapCamera 无法缩放到正确的高度

ios - 核心数据导致应用程序崩溃

iphone - 是否可以在Core Data中自动删除未引用的对象?

objective-c - 为所有 UIGestureRecognizer 添加目标

iOS/MKMapView : If one assigns to the image property of an MKAnnotationView an animated UIImage, 没有动画

ios - 如何用新的核心数据替换相同的字符串

swift - 核心数据性能问题

iphone - 如何在 iOS4 中发布图片

iphone - UIWebView shouldStartLoadWithRequest 只调用一次?

iphone - TabBar 应用程序是否需要 shouldAutorotateToInterfaceOrientation 在其所有 viewController 上返回 YES?