title: Swift array of dictionaries with multiple types without using NSDictionary in iOS
body: <p>I have been working with an array of dictionaries in Swift (version 7 beta 4) using NSDictionary.  The array is a collection of fmdb results which contain differing data types.  I would like to end up with an array of native swift dictionaries in order to use the filter functionality of swift collection types. The following is a snippet of the query function I'm using the create the array:</p>
  
  <pre><code>class func query(sql:String) -&gt; [NSDictionary] {
      let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask, true)
      let docsDir = dirPaths[0] as String
      let databasePath:String = docsDir.stringByAppendingPathComponent("myDB.db")
      let db = FMDatabase(path: databasePath as String)
      var resultsArray:[NSDictionary] = []
  
      if db.open() {
  
          let results:FMResultSet? = db.executeQuery(sql, withArgumentsInArray: nil)
          while (results?.next() == true) {
              resultsArray.append(results!.resultDictionary()) //appending query result
  
          }
          db.close()
  
      } else {
          print("Error: \(db.lastErrorMessage())")
      }
      return resultsArray
  }
  </code></pre>
  
  <p>I tried using AnyObject such as:</p>
  
  <pre><code>    class func query(sql:String) -&gt; [[String:AnyObject]] {
      let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask, true)
      let docsDir = dirPaths[0] as String
      let databasePath:String = docsDir.stringByAppendingPathComponent("Kerbal.db")
      let db = FMDatabase(path: databasePath as String)
      var resultsArray:[[String:AnyObject]] = []
  
      if db.open() {
  
          let results:FMResultSet? = db.executeQuery(sql, withArgumentsInArray: nil)
          while (results?.next() == true) {
              resultsArray.append(results!.resultDictionary() as! Dictionary&lt;String,AnyObject&gt;)
  
          }
          db.close()
  
      } else {
          print("Error: \(db.lastErrorMessage())")
      }
      return resultsArray
  }
  </code></pre>
  
  <p>however I can't use the filter such as <code>resultsArray.filter({$0["fieldName"] == "part"})</code> with <code>AnyObject</code>.</p>
  
  <p>My question: Is it possible to create this array with native Swift dictionaries even though the dictionaries have different types? Could the new protocol extension be used on collection type to solve this problem? </p>
  
  <p>Any suggestions are appreciated.</p>