[英]Filter json response in array of dictionary inside dictionary Swift iOS
我收到了NSDictionary产品列表的回复,
{
"products": {
"title": "Chair",
"regular_price": "2.22",
"dimensions": {
"width": "",
"height": "",
"length": ""
},
"attributes": [{
"options": ["11\/30\/2016"],
"name": "Arrival Date"
}, {
"options": ["Black"],
"name": "Color"
}],
"categories": ["28"]
}
} .....
使用NSPredicate,我可以使用过滤包含值“ Chair”的产品
let namepredicate = NSPredicate(format: "title == Chair")
self.filteredProducts = (self.product).filteredArrayUsingPredicate(namepredicate)
但是,如何过滤属性中的“颜色”,“黑色”和另一个数组(Swift)中的“黑色”呢?
首先,将self.product
重命名为self.products
。 它是多个产品的数组,请相应命名。
您可以将现有的NSPredicate
混乱替换为:
self.filteredProducts = self.products.filter{ $0["title"] == "Chair" }
您可以像这样按颜色过滤:
self.filteredProducts = self.products.filter{ product in
return product["attributes"]?.contains{ attribute in
return attribute["name"] == "Color" &&
attribute["options"]?.contains("Black") ?? false
} ?? false
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.