How to configure NSPredicate to perform filtering?

1

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

Table:

Forexample,returningtheelementsthathaveyesinthecolumnsATTRIBUTE1,ATTRIBUTE3,andATTRIBUTE5

InthisexampleIwouldreturntherecords:

linha1elinha3.

ItriedusingthefollowingconfigurationwithNSPredicate:

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

But this way you can only return the records when all the columns are identical to the NSPredicate setting.

    
asked by anonymous 26.02.2015 / 17:50

1 answer

2

In this case you need OR and not AND in your conditions. Your predicate would look something like this:

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

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

    
26.02.2015 / 18:38