0
I am creating a custom search, but I am creating the strings of the predicate as a Nsstring. However I am getting an error.
Code used:
NSMutableString* escolhasSIM = [[NSMutableString alloc]init];
// Posição 0
if ([[arrayEscolhas objectAtIndex:0] isEqualToString:@"sim"]) {
[escolhasSIM appendFormat:@"pizza==sim AND "];
}
// Posição 1
if ([[arrayEscolhas objectAtIndex:1] isEqualToString:@"sim"]) {
[escolhasSIM appendFormat:@"refrigerante==sim AND "];
}
// Remove os 5 últimos caracteres para limpar a string
NSString *escolhasSimLimpo = [escolhasSIM substringToIndex:[escolhasSIM length]-5];
// ficando @"pizza==sim AND refrigerante==sim"
// Criando NSPredicate
NSPredicate* argumentosBusca = [NSPredicate predicateWithFormat: escolhasSimLimpo];
But I’m getting the following error:
*** Terminating app due to uncaught Exception 'Nsinvalidargumentexception', Unable to generate SQL for predicate (pizza == yes) (problem on RHS)'
sim
is a string, so it needs to be inside of apostrophes, like"refrigerante == 'sim'"
. A tip, use it array (then do Join) instead ofNSMutableString
and after having to remove characters, your code gets cleaner.– Paulo Rodrigues
Thanks @Paulorodrigues I will review the code yes, it is because I am just working out a quick solution, then I will refine it.
– Tiago Amaral
I would recommend using
NSCompoundPredicate
in place ofNSMutableString
. In my opinion it would make the code cleaner and more meaningful.– Otávio