当我使用NSArray时NSPredicate崩溃

时间:2014-04-21 16:11:32

标签: ios objective-c core-data nsarray nspredicate

   +-----------+           +-----------+  
   | -Parada-  |           | -Autobus- |  
   +-----------+           +-----------+ 
   |  nombre   |           | circuito  | 
  +-------------+         +-------------+  
  |Relationships|         |Relationships|  
  +-------------+         +-------------+   
   |  byParada |<--------> |  parada   |
   +-----------+           +-----------+

我想使用setupFetchedResultsController中的核心数据获取一些数据,问题是在NSPredicate内部,当我想使用NSArray进行过滤时,它会崩溃。< / p>

这是代码:

NSArray  * myArray2 = [NSArray arrayWithObjects:@"1",@"2",@"3",nil];

NSString *predString = [NSString stringWithFormat:@"byParada.circuito IN %@", myArray2];
request.predicate = [NSPredicate predicateWithFormat:predString];

......结果如下:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "byParada.circuito IN (
    1,
    2,
    3
)"'

事实上,如果我删除myArray2的所有数字,只留下一个(NSArray * myArray2 = [NSArray arrayWithObjects:@"1",nil];)NSPredicate有效!

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

要解决这个问题,有两件事要做。首先,格式化的字符串应该在predicateWithFormat中,而不是在NSString中,然后传递给谓词。其次,在一对多关系中,您必须包含ANY或ALL。

request.predicate = [NSPredicate predicateWithFormat:@"ANY byParada.circuito IN %@", myArray2];