I'm trying to learn the wonderful world of NSPredicate, but am failing on practical application. Any guidance is appreciated.
In particular, I want to use NSPredicate to filter an array of Dictionaries. For example, take the following array and filter it down to just those entries where "species" == "dog".
var arrayofDictionary:[[String:String]] = [
["name": "Ben", "species": "human"],
["name": "Harp", "species": "dog"],
["name": "Guinness", "species": "dog"]
]
Now I know how to filter the following way, but my understanding is this is the "lazy" way to do it and will not run as fast when doing large computations:
let filteredArray = arrayofDictionary.filter { $0["species"] == "dog" }.flatMap { $0 }
So I have been playing around with NSPredicate, and feel like the following may be a good starting place, but am not sure how to use it next. Also, perhaps this only works for an array and not dictionaries?
let speciesPredicate = NSPredicate(format: "species == %@", "dog")
Thanks for your help!
I was only able to find guidance on this in Objective-C here. Unfortunately I only am starting to pick up Swift.
This was a good tutorial on NSPredicate to start to get my head wrapped around it, but I couldn't figure out how to fully implement for the case mentioned above.