我正在尝试在我的应用中实现以下代码,但会抛出异常。我需要从日期中提取年,月,日并将它们传递给谓词。例外是在行:
NSPredicate *yearPredicate = [NSPredicate predicateWithFormat:@"todoYear == %@", yearBuscado];
这是完整的方法代码:
- (NSFetchedResultsController *)fetchedResultsController
{
if (fetchedResultsController) return fetchedResultsController;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity =
[NSEntityDescription entityForName:@"FavoriteThing"
inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor =
[[NSSortDescriptor alloc] initWithKey:@"displayOrder"
ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc]
initWithObjects:sortDescriptor, nil];
//SOLO TO-DOS DE TODAY
todayDate = [NSDate date];
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:todayDate]; // Get necessary date components
NSString *yearStr = [NSString stringWithFormat:@"%ld", (long)[components year]];
NSString *monthStr = [NSString stringWithFormat:@"%ld", (long)[components month]];
NSString *dayStr = [NSString stringWithFormat:@"%ld", (long)[components day]];
NSString *hourStr = [NSString stringWithFormat:@"%ld", (long) [components hour]];
NSString *minuteStr = [NSString stringWithFormat:@"%ld", (long) [components minute]];
NSNumber *yearBuscado = (long)[components year];
NSNumber *mesBuscado = (long) [components month];
NSNumber *diaBuscado = (long) [components year];
NSString *tipourgente = @"Urgent";
NSString *tipocolor = @"Yellow";
NSPredicate *yearPredicate = [NSPredicate predicateWithFormat:@"todoYear == %@", yearBuscado];
NSPredicate *monthPredicate = [NSPredicate predicateWithFormat:@"todoMonth == %@", mesBuscado];
NSPredicate *dayPredicate = [NSPredicate predicateWithFormat:@"todoDay == %@", diaBuscado];
NSPredicate *urgentPredicate = [NSPredicate predicateWithFormat:@"urgent == %@", tipourgente];
NSPredicate *colorPredicate = [NSPredicate predicateWithFormat:@"color == %@", tipocolor];
[fetchRequest setSortDescriptors:sortDescriptors];
NSPredicate *busqueda = [NSCompoundPredicate andPredicateWithSubpredicates:@[yearPredicate, monthPredicate,dayPredicate,urgentPredicate]];
[fetchRequest setPredicate:busqueda];
NSFetchedResultsController *aFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
[self setFetchedResultsController:aFetchedResultsController];
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
return fetchedResultsController;
}
答案 0 :(得分:1)
问题在于:
NSNumber *yearBuscado = (long)[components year];
as [components year]
返回一个整数,而不是NSNumber
个对象。
它应该是
NSNumber *yearBuscado = [NSNumber numberWithLong:[components year]];
或更好,使用“盒装表达式”的新语法:
NSNumber *yearBuscado = @([components year]);
或者,使用
NSInteger yearBuscado = [components year];
并将您的谓词更改为
NSPredicate *yearPredicate = [NSPredicate predicateWithFormat:@"todoYear == %ld", (long)yearBuscado];