2011-09-26 45 views
0

當使用nspredicate時(謂詞之間)我有一個異常。謂詞之間的問題

有我使用的代碼:

NSMutableArray *returnedArray=[[[NSMutableArray alloc]init] autorelease]; 
NSManagedObjectContext *context = [self managedObjectContext]; 
NSEntityDescription *objEntity = [NSEntityDescription entityForName:@"Note" inManagedObjectContext:context]; 



NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
[fetchRequest setEntity:objEntity]; 


NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"When" ascending:YES]; 

NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 
[fetchRequest setSortDescriptors:sortDescriptors]; 
[sortDescriptor release]; 



NSPredicate *predicate1 = [NSPredicate predicateWithFormat: 
          @"self.Reunion==%@",reunion]; 
NSNumber *endOfEtape=[NSNumber numberWithInt:[etape.positionDepart intValue]+[etape.duree intValue]]; 
NSExpression *s1 = [ NSExpression expressionForConstantValue: etape.positionDepart ]; 
NSExpression *s2 = [ NSExpression expressionForConstantValue: endOfEtape ]; 
NSArray *limits = [ NSArray arrayWithObjects: s1, s2, nil ]; 
NSPredicate *predicate2=[NSPredicate predicateWithFormat: @"self.When BETWEEN %@",limits]; 

NSPredicate *predicate=[NSCompoundPredicate andPredicateWithSubpredicates: 
           [NSArray arrayWithObjects:predicate1,predicate2,nil]]; 
[fetchRequest setPredicate:predicate]; 


NSArray *notes; 
notes=[context executeFetchRequest:fetchRequest error:nil]; 

[fetchRequest release]; 

,我不得不在其上我稱之爲「executeFetchRequest:」行的「objc_exception_throw」方法。

如果你能幫助我,我會很高興。 謝謝。

回答

1

不幸的是,BETWEEN操作數被認爲是一個聚合函數&這些不受CoreData支持。

Apple's Documentation

...考慮BETWEEN運算(NSBetweenPredicateOperatorType);其右邊的 是一個包含兩個元素的集合。僅使用 Mac OS X v10.4 API,這些元素必須是常量,因爲沒有辦法使用變量表達式填充它們。在Mac OS X v10.4, 中,不可能在{$ YESTERDAY,$ TOMORROW};之間創建一個謂詞模板,其效果爲 。相反,您必須每次都創建一個新的謂詞謂詞 。

Core Data不支持聚合表達式。

所以你需要使用一個謂語,如:

NSPredicate *predicate2 = [NSPredicate predicateWithFormat: 
@"self.When >= %@ AND self.When <= %@", etape.positionDepart, endOfEtape]; 

(假設positionDepartendOfEtapeNSString型)

+0

positinDepart和endOfType是NSNumbers 所以我想我應該做的nstring包含兩個 –

+0

的值,你可以使用%d並獲取NSNumber的intValue ...無論如何它需要拆箱。 –