arrays - 如何谓词数组字典来查找匹配值

标签 arrays swift dictionary search nspredicate

我有以下字典:

let object = [[String : AnyObject]]()
/*
{  
"allProducts":[  
  {  
     "productDetails":[  
        {  
           "productName":"My product which I am unable to get by NSPredicate."
        }
     ]
  }
]
}
*/

我想通过谓词获取“productName”。

尝试过的代码:

let predicate = NSPredicate(format: "productName CONTAINS[c] '\(text)'")
arrayTableData = allProducts.filter() { predicate.evaluate(with: $0["productDetails"]) }

最佳答案

您不需要(也不应该)使用 NSPredicate 来完成如此简单的事情。

arrayTableData = allProducts.filter() { product in
    return product["productDetails"]?.contains { details in
        return details["productName"]?.contains(text) ?? false
    } ?? false
}

关于arrays - 如何谓词数组字典来查找匹配值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40904240/

相关文章:

javascript - struts2迭代器值到javascript数组

javascript - 2D JS 数组为每个索引分配值

iOS:当我需要在 View 对用户可见后执行某些操作时要覆盖哪个函数?

python - 如何将嵌套的字典键转换为字符串?

c# - 从字符串数组中删除列表中的内容

ios - 根据另一个数组重新排序 NSMutableArray

ios - 为什么在 tableViewCell 中点击一个按钮时只返回一个 uid?

ios - 如何在 alamofire 中检查互联网连接?

java - 二维映射中的迭代器

c# - 使用排序的整数数组键快速查找整数索引值