【问题标题】:NSPredicate with AND and OR issues带有 AND 和 OR 问题的 NSPredicate
【发布时间】:2012-11-15 15:13:05
【问题描述】:

我有以下 NSPredicate:

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

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您可以尝试这样做:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-06
        • 1970-01-01
        • 1970-01-01
        • 2011-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多