How to search from array of SwiftyJSON objects?

断了今生、忘了曾经 提交于 2019-12-05 14:16:30

Get the array from the JSON object with arrayObject, cast it to the proper type and apply the filter function, in this example to find the item whose name is def

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

Edit: For a refined search with NSPredicate and a JSON result use this

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)
}

You need to extract the dictionary Array from the JSON Object and the you can apply the Predicate to that array.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!