How to configure Nspredicate to perform filter?

Asked

Viewed 49 times

1

I am implementing a filter, where I need to search the records in Core Data, that have "yes" in certain columns.

Table:

Tabela exemplo

For example, return the elements that have yes in the columns ATRIBUTO1, ATRIBUTO3 and ATTRIBUTE 5

In this example would return the records:

linha1 e linha3. 

I tried using the following configuration with Nspredicate:

NSPredicate* argumentosBusca = [NSPredicate predicateWithFormat:@"atributo1 == %@&&atributo2 == %@&&atributo3 == %@&&atributo4 == %@&&atributo5 == %@",[arrayEscolhas objectAtIndex:0],[arrayEscolhas objectAtIndex:1],[arrayEscolhas objectAtIndex:2],[arrayEscolhas objectAtIndex:3],[arrayEscolhas objectAtIndex:4] ];

But this way only lets you return the records when all the columns are identical to the configuration of the NSPredicate.

  • I guess I didn’t understand it right. First you said to get the arguments that go with sim, nao, sim, nao e sim, and it’s just the flap1 that is so. But on the other hand you said you need results with value "yes".

  • Sorry. I’ll rephrase. I took a look, I revised a few times but it still wasn’t clear...

1 answer

2


In this case you need a OR and not AND on your terms. Your predicate would have something like this:

atributo1 == 'sim' || atributo3 == 'sim' || atributo5 == 'sim'

For in this case, there may be yes in any of the three columns and not exactly in the three, which will return you lines 1 and 3, as you quoted.

  • I did a test with || but I will check again because I saw an error in the search. I’ll see how it looks again.

  • It worked, the problem was that I changed the contents of the database, and did the wrong check. Now it’s ok!!

  • 1

    Another observation, I also switched the "não" with a x in strings that configure the search so that the comparison with "nao" were not carried out. Thus only the comparison with the yes would be considered.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.