how to solve NSPredicate error 'Can't use in/contains operator with collection 8 (not a collection)

五迷三道 提交于 2019-12-24 03:09:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!