title: How can I map a Firestore document to a struct?
body: <p>I have a Model that looks like this:</p>
  
  <pre><code>import Foundation
  import CoreLocation
  struct Address {
  
      //    let dateCreated: Date
      let street: String?
      let city: String?
      let state: String?
      let postalCode: String?
      let country: String?
      let ISOCountryCode: String?
      var coordinates: CLLocationCoordinate2D?
      var active: Bool?
  
      init?(dictionary: [String : Any]) {
          self.street = dictionary["street"] as? String
          self.city = dictionary["city"] as? String
          self.state = dictionary["state"] as? String
          self.postalCode = dictionary["postalCode"] as? String
          self.country = dictionary["country"] as? String
          self.ISOCountryCode = dictionary["ISOCountryCode"] as? String
          self.coordinates = dictionary["coordinates"] as? CLLocationCoordinate2D
          self.active = dictionary["active"] as? Bool
      }
  }
  </code></pre>
  
  <p>I've got a query to documents in Firebase Firestore. I want to map each document to the type of Object it should be; in this case an Address.</p>
  
  <p>For example, i <code>getDocuments</code> and then I start looping through each document in the snapshot.documents. My code (that isn't working) to map the document to a type of object looks like this:</p>
  
  <pre><code>let closure: (Dictionary&lt;String, Any&gt;) -&gt; String?
  closure = { dictionary in
      return Address(dictionary: dictionary)
  }
  var addresses = [Address]()
  
  addresses = document.data().flatMap(closure)
  </code></pre>
  
  <p>The errors I'm receiving are: </p>
  
  <p><code>Cannot assign value of type '([String : Any]) -&gt; Address?' to type '(Dictionary&lt;String, Any&gt;) -&gt; String?'</code></p>
  
  <p><code>Cannot convert value of type '(Dictionary&lt;String, Any&gt;) -&gt; String?' to expected argument type '((key: String, value: Any)) -&gt; String?'</code></p>
  
  <p>I'm sure it's something simple, I'm just not sure what I can do to fix it. :/</p>