title: Using a completion handler to return an array of items from a NASA API
body: <p>This is my working code to fetch one item from NASA API I used the completion handler as presented on Apple Programming book.</p>
  <pre><code>class PhotoInfoController {
      func fetchPhotoInfo(completion: @escaping (PhotoInfo?) -&gt; Void) {
          let baseURL = URL(string: &quot;https://api.nasa.gov/planetary/apod&quot;)!
  
      let query: [String:String] = [
          &quot;api_key&quot; : &quot;DEMO_KEY&quot;
      ]
  
      let url = baseURL.withQueries(query)!
  
      let task = URLSession.shared.dataTask(with: url) {
          (data, response, error) in
          let jsonDecoder = JSONDecoder()
  
          if let data = data,
             let photoInfo = try? jsonDecoder.decode(PhotoInfo.self, from: data) {
              completion(photoInfo)
          } else {
              print(&quot;Not found or data is not sanitazed.&quot;)
              completion(nil)
          }
      }
      task.resume()
     }
  }
  </code></pre>
  <p>The problem I having a hard time to figuring it out is how you can return an array go items (PhotoInfo) via a completion handler. This is my code so far:</p>
  <pre><code>class PhotoInfoController {
      func fetchPhotoInfo(completion: @escaping ([PhotoInfo]?) -&gt; Void) {
          let baseURL = URL(string: &quot;https://api.nasa.gov/planetary/apod&quot;)!
      let currentDate = Date()
      let formatter = DateFormatter()
      formatter.dateFormat = &quot;YYYY-MM-d&quot;
      var photoInfoCollection: [PhotoInfo] = []
      
      for i in 0 ... 1 {
          let modifiedDate = Calendar.current.date(byAdding: .day,value: -i ,to: currentDate)!
          let stringDate = formatter.string(from: modifiedDate)
          let query: [String:String] = [
              &quot;api_key&quot; : &quot;DEMO_KEY&quot;,
              &quot;date&quot; : stringDate
          ]
          
          let url = baseURL.withQueries(query)!
          let task = URLSession.shared.dataTask(with: url) { (data,
                                                              response,
                                                              error) in
              let jsonDecoder = JSONDecoder()
              
              if let data = data,
                 let photoInfo = try? jsonDecoder.decode(PhotoInfo.self, from: data) {
                  photoInfoCollection.append(photoInfo)
              } else {
                  print(&quot;Data was not returned&quot;)
              }
          }
          task.resume()
      }
      
      
      completion(photoInfoCollection)
      }
  }
  </code></pre>
  <p>Any ideas or guide will greatly appreciated Thanks!</p>
  <p>Code Implemented after suggestions:</p>
  <pre><code>class PhotoInfoController {
      private let baseURL = URL(string: &quot;https://api.nasa.gov/planetary/apod&quot;)!
      private let currentDate = Date()
      private let dateFormatter: DateFormatter = {
          let formatter = DateFormatter()
          formatter.dateFormat = &quot;YYYY-MM-d&quot;
          return formatter
      }()
      
      private let jsonDecoder = JSONDecoder()
      
      func fethPhotoInfo(itemsToFetch: Int, completion: @escaping ([PhotoInfo]?) -&gt; Void) {
          var count = 0
          var photoInfoCollection: [PhotoInfo] = []
          
          for i in 0 ... itemsToFetch {
              let modifiedDate = Calendar.current.date(byAdding: .day, value: -i, to: currentDate)!
              
              let query: [String : String] = [
                  &quot;api_key&quot; : &quot;DEMO_KEY&quot;,
                  &quot;date&quot; : dateFormatter.string(from: modifiedDate)
              ]
              
              let url = baseURL.withQueries(query)!
              let task = URLSession.shared.dataTask(with: url) {
                  (data, response, error) in
                  
                  if let data = data,
                     let photoInfo = try? self.jsonDecoder.decode(PhotoInfo.self, from: data) {
                      photoInfoCollection.append(photoInfo)
                      count += 1
                      
                      if count == itemsToFetch {
                          completion(photoInfoCollection)
                      }
                      
                  } else {
                      print(&quot;Data for \(self.dateFormatter.string(from: modifiedDate)) not made.&quot;)
                  }
              }
              task.resume()
          }
      }
  }
  </code></pre>