奇怪的iOS 5 FetchedResultsController谓词问题

时间:2013-04-03 01:32:59

标签: ios nsfetchedresultscontroller

此代码在iOS 6上运行正常,但在iOS 5上不会返回任何结果。使用类别方法将日期设置为今天。谁能发现任何奇怪的东西?谢谢!

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"KSJob"];

NSLog(@"today: %@", [NSDate today]);

// We only want to show upcoming jobs.
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"scheduledOn > %@", [NSDate today]]];

fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"scheduledOn" ascending:YES]];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                managedObjectContext:[(id)[[UIApplication sharedApplication] delegate]managedObjectContext]                                                 sectionNameKeyPath:@"dateToStringForSectionTitles"
                                                                           cacheName:@"UpcomingJobs"];
self.fetchedResultsController.delegate = self;

NSDate类别方法:

+ (NSDate*) today
{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]];
    components.timeZone = [NSTimeZone localTimeZone];
    components.hour = 0;
    components.minute = 0;

    NSDate *today = [cal dateByAddingComponents:components toDate:[[NSDate alloc] init] options:0];    
    return today;
}

1 个答案:

答案 0 :(得分:0)

好的,我想我找到了。由于某些原因,它似乎没有为iOS 5上的那些日期字段插入任何内容。这解释了很多。