NSPredicate与AND和OR问题

时间:2012-11-27 21:47:07

标签: iphone objective-c ios ipad core-data

我有以下NSPredicate:

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

我想用这个特定的URL和类型(突出显示或突出显示并保存)来获取故事。但是上面的查询似乎不起作用。我做错了什么?

2 个答案:

答案 0 :(得分:7)

就像将OR包装在括号中以避免混淆一样简单吗?

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

答案 1 :(得分:1)

您可以尝试这样做:

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