문제

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

I want to fetch some data using Core Data in the setupFetchedResultsController, the problem is that inside, in the NSPredicate, it crash when I want to filter using a NSArray.

This is the code:

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

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

...And the results are these:

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

In fact, if I erase all the numbers of myArray2 leaving just one ( NSArray * myArray2 = [NSArray arrayWithObjects:@"1",nil];) NSPredicate works!

Any help will be very appreciated.

도움이 되었습니까?

해결책

There are two things to do to fix that. First, the formatted string should be made in predicateWithFormat, not in a NSString which is then passed to the predicate. Second, in one-to-many relationships, you have to include either ANY or ALL.

request.predicate = [NSPredicate predicateWithFormat:@"ANY byParada.circuito IN %@", myArray2];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top