我的问题解决方法,但我真的想了解为什么我遇到这个问题以及如何解决它。
我有一个与另一个实体B相关的实体A,并且A的某些行在一个字段中有一个特殊标记。
我想要计算与B有关的A中有多少有这个特殊标记。
一切都很完美,如果在创建NSSet之后我计算了集合:
NSSet *productsSet = currentList.ingredients;
int countProducts = productsSet.count;
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"self IN %@ AND isInList = %d", productsSet,1];
[fetchRequest setPredicate:predicate];
int totalProductsInList = [context countForFetchRequest:fetchRequest error:error];
如果我发表评论 int countProducts = productsSet.count; 我有这些错误:
-[NSNull unsignedIntValue]: unrecognized selector sent to instance 0x22b9cd8
2011-12-05 12:10:41.418 xxxxxxxxxx[32964:1bb03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[NSNull unsignedIntValue]: unrecognized selector sent to instance 0x22b9cd8'
isInList是Int16
感谢,
如果我在NSPredicate中移动1,没有计数,我会得到同样的错误:
[NSPredicate predicateWithFormat: @"self IN %@ AND isInList = 1", productsSet;
由于我无法找到它为什么不起作用,我检查了productsSet.count,如果它大于0,我会解决NSFetchrequest问题。
答案 0 :(得分:0)
好吧,在创建NSSet之后我检查nsset.count,如果它大于零,我发出NSFetchrequest,如果它为零,则不会。
问题解决了,但如果我不使用nsset.count,我仍然对这个奇怪的错误有好奇心。
感谢,
答案 1 :(得分:0)
关系返回的NSSet对象不是普通的NSSet,而是_NSFaultingMutableSet类的对象。在NSPredicate中使用它似乎并不安全。可能会变成故障状态或其他什么。问题的原因与Core Data/NSOperation: crash while enumerating through and deleting objects中的相同。
所以最好的方法是使用allObjects的结果或创建一个新的NSSet对象:
NSArray *productsArray = [currentList.ingredients allObjects];
或
NSSet *productsSet = [NSSet setWithSet:currentList.ingredients];