2016-12-01 46 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

相關問題