Arun K Arun K - 5 months ago 12
iOS Question

Search within dictionary in Swift

I'm getting

NSDictionary
from JSON response

How can search within an
NSDictionary
of
Arrays
.Each array contains a
NSDictionary
. I want to search for particular casino name but i also want to get the address field with associated with it . How can i get it in swift ios.

The JSON response is below

{
"success": [
{
"casino_id": "2",
"casino_name": "cas",
"address": "add",
"distance": "0.19084827576745822"
},
{
"casino_id": "4",
"casino_name": "eeee",
"address": "adressdd",
"distance": "0.12319974564234398"
}
]
}


code for searching iss below

var casinoarray = NSArray() // CONTAINS THE JSON
var resultsarray = NSArray() // USED FOR FILTERED ARRAY
let resultPredicate = NSPredicate(format: "self.casino_name == %@", searchText)

resultsarray = casinoarray.filteredArrayUsingPredicate(resultPredicate)
tableView.reloadData()
print(resultsarray)


Please help me with it.

TIA

Answer

You can use like this

        let prdicate:NSPredicate = NSPredicate(format: "self.casino_name contains[c] %@", searchText)

        let filterArray = self.yourArray.filter({
            prdicate.evaluateWithObject($0)
        })
        print(filterArray)
Comments