2016-12-01 71 views
0

我有以下的解释:我如何能断定一个数组字典查找匹配的值

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

我想谓词来了“产品名称”。

代码尝试:

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

你可以使用swift语法编写你的'object'的内容吗?我无法阅读JSON – Sweeper

+0

'对象'将具有注释的JSON对象。 – Lazy

+0

我知道,但我在阅读JSON时很不好... – Sweeper

回答

0

您不需要(也不应该)使用NSPredicate的东西这么简单。

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

但'productDetails'有一个字典数组,字典有一个键“产品名称”。 – Lazy

+0

等待,每个productDetails是一个包含单个键的字典数组:值对每个? ...为什么? – Alexander

+0

答复更详细,我刚写下需要提取的值。 – Lazy

相关问题