NSPredicate with AND and OR questions

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 get history with that specific url and type (highlighted or highlighted and saved). However, the above query doesn't work. What am I doing wrong?

0


source to share


2 answers


Not as easy as wrapping your OR in parentheses to keep confusion?



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

      

+7


source


You may try:



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

      

+1


source







All Articles