问题
I tried to predicate my coredate based on the mood on the initial queries all the moods are set to 8. I will be calling from 0 - 7 every sec to update the tableview.
but I got
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't use in/contains operator with collection 8 (not a collection)'
Should I use CONTAINS or other operator to predicate?
NSString* filter = @"%K CONTAINS[cd] %@";
NSPredicate *getMoodPred = [NSPredicate predicateWithFormat:filter, @"mood", mood];
NSLog(@"getmood %@",getMoodPred); //getmood mood CONTAINS[cd] "7"
NSArray *getMoodArray = [[VPPCoreData sharedInstance]allObjectsForEntity:@"Song" orderBy:Nil filteredBy:getMoodPred];
回答1:
The probably means that the mood
attribute is stored as Number, not as String. "CONTAINS" works only with strings. The following predicate should work:
NSNumber *mood = @(7);
NSString *filter = @"%K == %@";
NSPredicate *getMoodPred = [NSPredicate predicateWithFormat:filter, @"mood", mood];
using NSNumber
on the rhs and ==
as comparator.
来源:https://stackoverflow.com/questions/20606453/how-to-solve-nspredicate-error-cant-use-in-contains-operator-with-collection-8