Pregunta

I have a NSManagedObject with the following attributes:

  • status
  • kind
  • priority

Now I want to be able to filter my entity with these attributes respectively. So I would expect that I have to have a predicate along those lines:

status CONTAINS[c] ‘open’

I get really weird results, as soon as I have two variables in my predicate and I have to reverse the order of kind and value in my case so that I get the desired results:

NSString *kind = @"status"; // DEBUGGING
NSString *value = @"open"; // DEBUGGING

// This works although it defies all logic
NSString *predicate = [NSString stringWithFormat:@"('%@' CONTAINS[c] %@)", value, kind];
self.myFilterPredicate = [NSPredicate predicateWithFormat:predicate];

This however, does not work for some reason:

NSString *predicate = [NSString stringWithFormat:@"(%@ CONTAINS[c] ‘%@‘)”, kind, value];
¿Fue útil?

Solución

I cannot reproduce the exact problem, but generally you should not use stringWithFormat to create predicate. It causes problems as soon as the substituted key or value contain any special characters like spaces or quotation marks.

A better way is

self.myFilterPredicate = [NSPredicate predicateWithFormat:@"%K == %@", kind, value];

%K is a placeholder to be replaced by a key path such as "status".

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top