title: issue passing data to segue.destination within function
body: <p>I'm trying to make 2 API calls on Segue invoke and ultimately pass Array of Data from Second Call to <code>CollectionView</code>. With first call I'm getting one value <strong><code>catID</code></strong>, which I need in order to make the other call: </p>
  
  <pre><code>let searchEndpoint: String = MY_ENDPOINT
  
  // Add auth key
  let serviceCallWithParams = searchEndpoint + "?PARAMETER"
  guard let url = URL(string: serviceCallWithParams) else {
      print("Error: cannot create URL")
      return
  }
  
  let urlRequest = URLRequest(url: url)
  
  // setting up the session
  let config = URLSessionConfiguration.default
  let session = URLSession(configuration: config)
  
  // making the request
  let task = session.dataTask(with: urlRequest) {
      (data, response, error) in
      // error check
      guard error == nil else {
          print("error")
          print(error)
          return
      }
      // make sure we got data
      guard let responseData = data else {
          print("Error: did not receive data")
          return
      }
      // parse JSON
      do {
          guard let catData = try JSONSerialization.jsonObject(with: responseData, options: []) as? [String: AnyObject] else {
              print("error converting data to JSON")
              return
          }
          if let data = catData["data"] as? [String: Any] {
              if let array = data["categories"] as? [Any] {
                  if let firstObject = array.first as? [String: Any] {
                      if let catId = firstObject["catId"] as? Int {
                          getTitles(catId: catId)
                      }
                  }
              }
          }
  
      } catch  {
          print("error converting data to JSON")
          return
      }
  }
  task.resume()
  </code></pre>
  
  <p><strong>And then getTitles function looks like this:</strong></p>
  
  <pre><code>func getTitles(catId: Int) {
      let catIdString = String(catId)
      let titlesEndPoint: String = MY_ENDPOINT + catIdString
      // Add auth key
      let titlesEndPointWithParams = titlesEndPoint + "?PARAMETER"
      guard let titlesUrl = URL(string: titlesEndPointWithParams) else {
          print("Error: cannot create URL")
          return
      }
  
      let titlesUrlRequest = URLRequest(url: titlesUrl)
  
      // set up the session
      let config = URLSessionConfiguration.default
      let session = URLSession(configuration: config)
  
      // make the request
      let task = session.dataTask(with: titlesUrlRequest) {
          (data, response, error) in
          // check for any errors
          guard error == nil else {
              print("error calling GET on listCategoryTitles")
              print(error)
              return
          }
          // make sure we got data
          guard let titlesData = data else {
              print("Error: did not receive data")
              return
          }
          // parse the JSON
          do {
              guard let allTitles = try JSONSerialization.jsonObject(with: titlesData, options: []) as? [String: AnyObject] else {
                  print("error converting data to JSON")
                  return
              }
              if let titlesJson = allTitles["data"] as? [String: Any] {
                  if let titlesArray = titlesJson["titles"] as? Array&lt;AnyObject&gt; {
                      self.books = []
                      for (index, value) in titlesArray.enumerated() {
                          var book = Book()
                          book.bookTitle = value["title"] as? String
                          book.bookAuthor = value["author"] as? String
                          if let imageSource = value["_links"] as? Array&lt;AnyObject&gt; {
                              book.bookImageSource = imageSource[1]["href"] as? String
                          }
                          self.books?.append(book)
                      }
                  }
              }
  
          } catch  {
              print("error converting data to JSON")
              return
          }
      }
      task.resume()
  }
  </code></pre>
  
  <p><strong>Now when I put:</strong></p>
  
  <pre><code>let resultsVC = segue.destination as? CollectionViewController
  resultsVC?.books = self.books
  </code></pre>
  
  <p>outside function, in target controller I'm getting an empty array as output on first click, but on every next one I'm getting proper data.
  When I try putting this inside function <strong><em>"getTitles"</em></strong> the output in CollectionViewController is <strong><em>"nil"</em></strong> every time.
  Worth mentioning could be that I have <strong><em>"books"</em></strong> variable defined like so:</p>
  
  <p><strong>Main Controller:</strong></p>
  
  <pre><code>var books: [Book]? = []
  </code></pre>
  
  <p><strong>Collection Controller:</strong></p>
  
  <pre><code>var books: [Book]?
  </code></pre>
  
  <p>and I have created type [Book] which is basically object with 3 string variables in separate struct.</p>
  
  <p><strong>All of the code above is encapsulated in</strong></p>
  
  <pre><code>override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if segue.identifier == "ShowResults" {
  </code></pre>
  
  <p>Any help/guideline would be much appreciated!</p>