4
votes

I've read recently about how casting work in NSPredicate and I'm trying to use one in my FetchResultController. The result is so unexpected:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'unimplemented SQL generation for predicate : (CAST(orderNumber, "NSString") CONTAINS[cd] "%1$@") (bad RHS)'

The orderNumber is NSNumber. It contains an auto-generated id get from the server. I'm not sure if I can use CAST in the FetchResultController, since some answer in SO said that it's able to use CAST in normal NSArray.

Here's my code:

NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"localOrderId contains[cd] '%1$@' "
                                        "OR customerName contains[cd] '%1$@' "
                                        "OR (CAST(orderNumber, 'NSString') contains[cd] '%1$@') ", keySearch];            

[_fetchedResultsController.fetchRequest setPredicate:searchPredicate];

NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) { // << Die here
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
1

1 Answers

1
votes

Instead of CAST use orderNumber.stringValue or orderNumber.description. It works.

NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"localOrderId contains[cd] '%1$@' "
                                        "OR customerName contains[cd] '%1$@' "
                                        "OR orderNumber.stringValue contains[cd] '%1$@') ", keySearch];