Ios NSPredicate:使用filteredArrayUsingPredicate筛选NSDateComponents数组时发送到实例的无法识别的选择器
我有一些代码,其中包含从现在到一年的日期数组Ios NSPredicate:使用filteredArrayUsingPredicate筛选NSDateComponents数组时发送到实例的无法识别的选择器,ios,nsarray,nsdate,nspredicate,nsdatecomponent,Ios,Nsarray,Nsdate,Nspredicate,Nsdatecomponent,我有一些代码,其中包含从现在到一年的日期数组 NSCalendar* calendar = [NSCalendar currentCalendar]; NSMutableArray *dateComponentsInRange = [NSMutableArray new]; NSDate *curDate = [dates objectAtIndex:0]; while ([curDate timeIntervalSince197
NSCalendar* calendar = [NSCalendar currentCalendar];
NSMutableArray *dateComponentsInRange = [NSMutableArray new];
NSDate *curDate = [dates objectAtIndex:0];
while ([curDate timeIntervalSince1970] <= [[dates lastObject] timeIntervalSince1970])
{
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:curDate];
[dateComponentsInRange addObject:components];
curDate = [curDate dateWithNextDay];
}
但是我得到了以下错误
[__NSCFString evaluateWithObject:substitutionVariables:]: unrecognized selector sent to instance
将字符串添加到数组并将其用作谓词 尝试:
根据Apple Docs
orPredicateWithSubpredicates
的说法,它需要一个NSPredicate
数组,但您传递的是一个NSString
对象数组
错误是
[\uu NSCFString evaluateWithObject:substitutionVariables::]:发送到实例的选择器无法识别
这是正确的,因为evaluateWithObject:substitutionVariables
不是为NSString
定义的,而是为NSPredicate
Src:Apple Documentation确保没有零日期。您错过了错误消息的重要部分。剩下的怎么说?数组中没有nil日期,我甚至用数组中的一个非nil元素尝试过,但仍然得到了错误,这就是我得到的所有消息
[__NSCFString evaluateWithObject:substitutionVariables:]: unrecognized selector sent to instance
NSDateComponents *components = nil;
NSMutableArray *predicates = [NSMutableArray array];;
for (NSDate *currentdate in dates)
{
components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentdate];
NSString *preficateFormat = [NSString stringWithFormat:@"(SELF.day != %d AND SELF.month != %d AND SELF.year != %d)",[components day],[components month], [components year]];
NSPredicate * predicate = [NSPredicate predicateWithFormat:preficateFormat];
[predicates addObject:predicate];
}
NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:predicates];
NSArray *filteredArr = [dateComponentsInRange filteredArrayUsingPredicate:predicate];