如何解决 NSPredicate 错误“不能将 in/contains 运算符与集合 8(不是集合)一起使用" [英] how to solve NSPredicate error 'Can't use in/contains operator with collection 8 (not a collection)

查看:79
本文介绍了如何解决 NSPredicate 错误“不能将 in/contains 运算符与集合 8(不是集合)一起使用"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试根据初始查询的情绪来预测我的核心日期,所有情绪都设置为 8.我将每秒钟从 0 - 7 调用一次以更新 tableview.

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.

但是我得到了

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't use in/contains operator with collection 8 (not a collection)'

我应该使用 CONTAINS 或其他运算符来做谓词吗?

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];

推荐答案

这可能意味着 mood 属性存储为 Number,而不是 String.CONTAINS"仅适用于字符串.以下谓词应该有效:

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];

在 rhs 上使用 NSNumber== 作为比较器.

using NSNumber on the rhs and == as comparator.

这篇关于如何解决 NSPredicate 错误“不能将 in/contains 运算符与集合 8(不是集合)一起使用"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆