【问题标题】:NSPredicate searching NSSetNSPredicate 搜索 NSSet
【发布时间】:2013-04-03 09:01:45
【问题描述】:

我一直在环顾四周,看到了与我正在做的事情类似的例子,但无法让它们发挥作用。我有一个“产品”核心数据实体,它与“制造商”实体有一对多关系。 “制造商”有一个我要搜索的属性“名称”。 “产品”还有一个我希望搜索的“isCustomItem”属性。所以我想要达到的效果如下:

Product 1...m Manufacturer.name AND Product.isCustomItem == 0

这就是我迄今为止设法走到一起的:

NSPredicate *p3 = [NSPredicate predicateWithFormat:@"SUBQUERY(manufacturer,$m,$m.name CONTAINS[c] %@) AND (isCustomItem == 0)", searchString];

但是我不断收到错误:

**'Unable to parse the format string "SUBQUERY(manufacturer,$m,$m.name CONTAINS[c] %@) AND (isCustomItem == 0)"'**

【问题讨论】:

    标签: objective-c regex full-text-search nspredicate


    【解决方案1】:

    试试这样的...

    NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"ANY manufacturer.name CONTAINS[c] %@", searchString];
    NSPredicate *customPredicate = [NSPredicate predicateWithFormat:@"isCustomItem == 0"];
    
    NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubPredicates:@[namePredicate, customPredicate]];
    

    然后使用 CompoundPredicate 过滤您的集合。

    【讨论】:

    • 感谢您的快速回复。这样做时我仍然收到此错误:'无法解析格式字符串“任何制造商.name CONTAINS [c] @%”'
    • 应该是%@,而不是@%
    • 呸,不敢相信我也没听懂。已经解决了,谢谢你的帮助!
    猜你喜欢
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 2011-03-25
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    相关资源
    最近更新 更多