+-----------+ +-----------+
| -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有效!
非常感谢任何帮助。
答案 0 :(得分:1)
要解决这个问题,有两件事要做。首先,格式化的字符串应该在predicateWithFormat中,而不是在NSString中,然后传递给谓词。其次,在一对多关系中,您必须包含ANY或ALL。
request.predicate = [NSPredicate predicateWithFormat:@"ANY byParada.circuito IN %@", myArray2];