0

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?

adit
  • 32,574
  • 72
  • 229
  • 373

2 Answers2

7

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]];
Rawkode
  • 21,990
  • 5
  • 38
  • 45
1

You can try to do this:

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"url = '%@' AND( type = %@ OR type = %@"), newsStory.url, [NSNumber numberWithInt:StoryHighlighted], [NSNumber numberWithInt:StorySavedAndHighlighted]];
Anthon
  • 69,918
  • 32
  • 186
  • 246
YDT
  • 11
  • 1