Reputation: 135
I have this code:
[
{
"id": 176,
"aSegments": [
"CUT_SPECIALITIES1",
"CUT_SPECIALITIES2",
"CUT_SPECIALITIE2S"
],
"sunFl": true,
"inPi": false,
"noBox": 72,
"prepOven": "15 min / 220C"
}
]
let searchString = "CUT_SPECIALITIES2"
let products = DataMgr.instance.getProducts(reload: false)
var productsObjectArray = products.filter({
return (($0.aSegments?.filter({
$0.description.lowercased().contains(searchString.lowercased())
}))?.count)! > 0
})
How can I search CUT_SPECIALITIES2
to find the searched string?
Upvotes: 1
Views: 110
Reputation: 4891
This is the array I assumed you have :
let array = [
[
"id" : 176,
"aSegments": [
"CUT_SPECIALITIES1",
"CUT_SPECIALITIES2",
"CUT_SPECIALITIE2S"
],
"sunFl": true,
"inPi": false,
"noBox": 72,
"prepOven": "15 min / 220C"
],
[
"id" : 177,
"aSegments": [
"CUT_SPECIALITIES1",
"CUT_SPECIALITIES2",
"CUT_SPECIALITIE2S"
],
"sunFl": true,
"inPi": false,
"noBox": 73,
"prepOven": "18 min / 120C"
]
]
And from this Array of Dictionaries
, you want to filter the dictionaries based on the searchString
.
let searchString = "CUT_SPECIALITIES2"
let filteredArray = array.filter { (singleProduct) -> Bool in
guard let aSegments = singleProduct["aSegments"] as? [String] else {
return false
}
return (aSegments.filter({ $0 == searchString }).count > 0)
}
This gives an Array of Dictionaries
which contains your searched string.
Upvotes: 2