【问题标题】:XCode Magical Record, MR_findByAttribute - looking for strings with certain word (regex?)XCode Magical Record, MR_findByAttribute - 查找带有特定单词的字符串(正则表达式?)
【发布时间】:2014-09-30 13:51:38
【问题描述】:

我有一个 API,我从中加载 Shop 对象。 Shop 对象具有 NSString *assortment 字段,包含您可以在商店购买的产品,在我的语法中它是例如。 “水、牛奶、土豆、面包、番茄酱”。

NSString *assortment = @"water, milk, potatoes, bread, ketchup";

我将商店从本地数据库加载到 NSArray *shops。

NSArray *shops = [Shop MR_findAll];

我想在我的商店数组中找到所有在其分类字段中包含单词“面包”的对象(也许使用 MR_findByAttribute?)。

我知道我可以拆分我的字符串并检查每个分类位置 - 但这对我来说听起来不直观。我觉得有更好的解决方案,也许:

NSArray *shopsWithBread = [Shop MR_findByAttribute:@"assortment" withValue:@"<bread regex?>"];

你知道有什么好方法吗?您是否还有其他好主意如何找到所有这些商店?也许你确定不能这样,我必须自己写搜索引擎?

提前感谢您的所有回答。 卡罗尔

【问题讨论】:

    标签: ios objective-c xcode magicalrecord


    【解决方案1】:

    好的,我找到了解决方案 - 我正在使用谓词。

    NSPredicate *breadSearchPredicate = [NSPredicate predicateWithFormat:@"assortment contains[cd] %@", @"bread"];
    NSArray *shopsWithBread = [Shop MR_findAllWithPredicate:breadSearchPredicate];
    

    分类是我的字段名称; contains[cd] 是一种指令,检查是否包含字符串给 - 这里是@"bread"。

    NSPredicate - 在这里很好地解释了:http://nshipster.com/nspredicate/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      相关资源
      最近更新 更多