adit
adit

Reputation: 33644

NSPredicate with AND and OR issues

I have the following NSPredicate:

 fetchRequest.predicate = [NSPredicate predicateWithFormat:@"url = %@ AND type = %@ OR type = %@", newsStory.url, [NSNumber numberWithInt:StoryHighlighted], [NSNumber numberWithInt:StorySavedAndHighlighted]];

I wanted to fetch a story with this particular URL and type of (highlighted or highlightedandsaved). However the query above doesn't seem to work. What am I doing wrong?

Upvotes: 0

Views: 2669

Answers (2)

YDT
YDT

Reputation: 11

You can try to do this:

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"url = '%@' AND( type = %@ OR type = %@"), newsStory.url, [NSNumber numberWithInt:StoryHighlighted], [NSNumber numberWithInt:StorySavedAndHighlighted]];

Upvotes: 1

Rawkode
Rawkode

Reputation: 22582

As simple as wrapping your OR in brackets to save any confusion?

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"url = %@ AND (type = %@ OR type = %@)", newsStory.url, [NSNumber numberWithInt:StoryHighlighted], [NSNumber numberWithInt:StorySavedAndHighlighted]];

Upvotes: 7

Related Questions