Marcal
Marcal

Reputation: 1371

NSPredicate not responding

I'm using Stanford's UICoreDataTableViewController to perfom a fetch on the database.

On my main view, the user enters a float on a searchBar and by tapping on search, the predicate makes it so it only fetches values between a max and a minimum (i.e. floats between 3 and 10).

In the main view, I build a string with the entity, a string with the attribute where the search must be performed and finally an array with the predicates (more than x and lower than y). I pass the strings and the array on prepareForSegue. It works pretty well.

I want the user to be able to see all the vaules if he/she doesn't type any number. So in the prepareForSegue I added a conditional which is called correctly according to NSLog.

The problem is that on the attibutes some values are just nil (I made sure of that). If of prepareForeSegue I pass the following predicate array:

prepredicateArrayToPass=[NSArray arrayWithObject:[NSPredicate predicateWithFormat:@"%@ !=nil", attributeToPass]];

the fetch is done interpreting the nil values as 0 and therefore being displayied. Not good.

However if I copy the predicate and I paste it directly on the setupFetchedResultsController method on UICoreDataTableViewController it works perfectly and the nil values are not displayied.

If I add the conditional directly on the UICoredataTableViewController, it doesn't work either.

I really don't know how to go further on that. Any help would be much appreciated.

Thanks.

Upvotes: 0

Views: 151

Answers (1)

Marcal
Marcal

Reputation: 1371

So, I found the answer within minutes of posting the question.

The problem was a simple syntax on the predicate string. Instead of using a %@ to designate the attibute it should be a %K. Everything is explained on the documentation.

so, the predicate array should be:

prepredicateArrayToPass=[NSArray arrayWithObject:[NSPredicate predicateWithFormat:@"%K !=nil", attributeToPass]];

Now works as a charm.

Upvotes: 2

Related Questions