Domanda

   +-----------+           +-----------+  
   | -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.

È stato utile?

Soluzione

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];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top