title: NSPredicate to filter an Array of Dictionaries (Swift)
body: <p>I'm trying to learn the wonderful world of NSPredicate, but am failing on practical application.  Any guidance is appreciated.</p>
  
  <p>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".</p>
  
  <pre><code> var arrayofDictionary:[[String:String]] = [
  
        ["name": "Ben", "species": "human"], 
        ["name": "Harp", "species": "dog"], 
        ["name": "Guinness", "species": "dog"]
  
   ]
  </code></pre>
  
  <p>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:</p>
  
  <pre><code> let filteredArray = arrayofDictionary.filter { $0["species"] == "dog" }.flatMap { $0 }
  </code></pre>
  
  <p>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?</p>
  
  <pre><code> let speciesPredicate = NSPredicate(format: "species == %@", "dog")
  </code></pre>
  
  <p>Thanks for your help!</p>
  
  <hr>
  
  <p>I was only able to find guidance on this in Objective-C <a href="https://stackoverflow.com/questions/21201251/nspredicate-for-array-of-dictionaries">here</a>.  Unfortunately I only am starting to pick up Swift.</p>
  
  <p>This was <a href="http://nshipster.com/nspredicate/" rel="nofollow noreferrer">a good tutorial on NSPredicate</a> to start to get my head wrapped around it, but I couldn't figure out how to fully implement for the case mentioned above.</p>