Illep
Illep

Reputation: 16841

Writing a NSPredicate for my scenario

I have a predicate and i need to filter out an attribute. The name of the attribute is location, and this attribute could have none, one of more values. So how could i write the predicate

for example if the hospital location could have 0 or more values. It could have no location, or 1 (New York) or more (New York, California, Texas ). So based on the values on the array how could i write the predicate.

NSArray *filteredHospitalArr= [hospitalArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"location= %@ ", [hospitalObject objectAtIndexPath:0]]]; // ???

<-- Say at this instance we have 3 locations for this Hospital Location. How can i write this predicate.

Also note, that the number of location might change, (it might have none, one or more values)

EDIT:

Say if the user is asked to choose his/her filter criteria for a particular hospital. He/she would select one or more locations. So how do i write the predicate to suit his/her filter requirement. (The user might or might not enter a filter criteria, or enter more than one preferred location). So how could i write it based on this scenario

Upvotes: 0

Views: 57

Answers (2)

wattson12
wattson12

Reputation: 11174

do you want to filter out hospitals with no locations?

[NSPredicate predicateWithFormat:@"locations.@count != 0"]

update: maybe something more like

@"ANY locations IN %@", myArrayOfLocations"

Upvotes: 1

andi1984
andi1984

Reputation: 676

Maybe the Apple Predicate Programming Guide can help you?

Upvotes: 1

Related Questions