How to search from an array of SwiftyJSON objects?

I have a SwiftyJSON object.

How to search by key (name) from an array of SwiftyJSON object .

Example:

let arrCountry = [JSON]()

Now one array of countries contains

{
    countryid : "1"
    name : "New York"
},
{
    countryid : "2"
    name : "Sydeny"
}

How to search by predicate or something else?

If I look for "y" from an array, then it should return both objects (because both contain "y") in another JSON array. And if I type "Sid", then it should return only one object in the JSON array. Like a LIKE query in SQL. And how do we do with predicates ...

+4
source share
2 answers

JSON arrayObject, filter , , name def

if let array = arrCountry.arrayObject as? [[String:String]],
   foundItem = array.first(where: { $0["name"] == "def"}) {
   print(foundItem)
}

: NSPredicate JSON

let searchText = "y"
let searchPredicate = NSPredicate(format: "name contains[cd] %@", searchText)
if let array = arrCountry.arrayObject as? [[String:String]] {
    let foundItems = JSON(array.filter{ searchPredicate.evaluateWithObject($0) })
    print(foundItems)
}
+6

JSON, Predicate .

0

All Articles