title: Converting URL to UIImage. Able to convert but not append to UIImage array
body: <p>I'm making an application using swift that calls onto a Google Places api to generate a JSON file of locations, including images of the location generated. These images are given as URLs that I need to convert to UIImage and then append these images to an array. When opening the contents of the URL, I am able to see the images, but these images are not appending to the array. Here is my view controller's class that is attempting to generate said images:</p>
  
  <pre><code>import Foundation
  import UIKit
  import WebKit
  
  class ImageViewController: UIViewController {
  
      @IBOutlet weak var imageView: UIImageView!
      var photos: [Photo]?
      var uiImages: [UIImage]?
  
      override func viewDidLoad() {
  
          for photo in photos! {
  
              let url = URL(string:"https://maps.googleapis.com/maps/api/place/photo?photoreference=\(photo.reference)&amp;sensor=false&amp;maxheight=\(photo.height)&amp;maxwidth=\(photo.width)&amp;key=AIzaSyC_SoYT7VnYnyz3GAb7qqbXjZeLFG5GE70")
  
              let data = try? Data(contentsOf: url!)
  
              let image: UIImage = UIImage(data: data!)!
  
              self.uiImages?.append(image)
  
              print(image)
  
              print(self.uiImages)
  
          }
      }
  }
  </code></pre>
  
  <p>In this loop, I tell the code to print "image" and then the array "uiImages" after the append occurs. Yet, I am returning nil when printing the array of images, but not nil for the image itself. 
  <a href="https://i.stack.imgur.com/D9Aal.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D9Aal.png" alt="enter image description here"></a></p>
  
  <p>I feel this could have something to do with the asynchrony of the method, but I also tried appending on the main thread and this did not change anything. Additionally, the "photos" variable is not nil, it is set when the view controller is instantiated.</p>
  
  <p>Here is the code for the Photo class:</p>
  
  <pre><code>import Foundation
  
  struct Photo {
  
      var height: Int
      var width: Int
      var reference: String
  
      init?(height: Int, width: Int, reference: String) {
          self.height = height
          self.width = width
          self.reference = reference
      }
  }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>Here is what my ImageViewController class looked like after making the suggested changes:</p>
  
  <pre><code>import Foundation
  import UIKit
  import WebKit
  
  class ImageViewController: UIViewController {
  
      @IBOutlet weak var imageView: UIImageView!
      var photos: [Photo]?
      var uiImages = [UIImage]()
  
      override func viewDidLoad() {
  
          for photo in photos! {
  
              let url = URL(string:"https://maps.googleapis.com/maps/api/place/photo?photoreference=\(photo.reference)&amp;sensor=false&amp;maxheight=\(photo.height)&amp;maxwidth=\(photo.width)&amp;key=AIzaSyC_SoYT7VnYnyz3GAb7qqbXjZeLFG5GE70")
  
              let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
  
                  let image: UIImage = UIImage(data: data!)!
  
                  self.uiImages.append(image)
  
                  print(image)
  
                  print(self.uiImages)
              }
  
              task.resume()
          }
      }
  }
  </code></pre>