title: Why does an instance variable have a different value inside of a closure?
body: <pre><code>func loadYelpComments(){
          guard let business = business else {return}
          guard let _ = tableView else {return}
          guard let businessID = business.id else {return}
          let dateFormatter = DateFormatter()
          dateFormatter.dateStyle = .full
  
          HTTPHelper.getYelpComments(for: businessID, completionHandler: { data in
              let json = JSON(data)
              let reviews = json["reviews"].arrayValue.map({return $0.dictionaryValue})
              var commentDate: Date?
  
  
              for (index, review) in reviews.enumerated(){
                  let userDictionary = review["user"]?.dictionary
                  if let dateString = review["time_created"]?.stringValue{
                      commentDate = dateFormatter.date(from: dateString)
                  }
  
                  let yelpComment = YelpComment(rating: review["rating"]?.intValue, userImageURL: userDictionary?["image_url"]?.stringValue, userName: userDictionary?["name"]?.stringValue, comment: review["text"]?.stringValue, commentDate: commentDate, commentPageURL: review["url"]?.stringValue)
  
                  self.comments.append(yelpComment)
              }
  
              print("Number of comments: \(self.comments.count)") //Prints: Number of comments: 3"
              self.tableView.reloadData()
  
          })
  
              print("Number of comments: \(self.comments.count)") //This prints firt "Number of comments: 0"
  
      }
  </code></pre>
  
  <p>The <code>getYelpComments(for:completionHandler:)</code> class method is responsible for fetching JSON data from the Yelp API. To my surprise even though the <code>comments</code> instance array is being updated by appending new <code>YelpComment</code> objects to it, its <code>count</code> has different values inside and outside of the closure.</p>
  
  <p>These comments are supposed to be displayed in a table view. What further confuses me is the fact that when I add <code>tableView.reloadData()</code> within the closure I get an <code>index out of bounds for an empty array</code> error but when I add the same line of code: <code>tableView.reloadData()</code> out side of the closure I get no error and <code>comments.count</code> equates zero. Any help would be much appreciated! </p>
  
  <p>P.S. I don't know if mentioning this is going to make any difference but <code>data</code> is <code>@escaping</code>. I am also using <code>SwiftyJSON</code> and <code>Alamofire</code>.</p>
  
  <p>UPDATE: </p>
  
  <p>My table view data source methods are: </p>
  
  <pre><code>   override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
  
          if section == 0 {
              return super.tableView(tableView, numberOfRowsInSection: section)
          } else {
              return comments.count
          }
  
      }
  
      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
  
          if indexPath.section == 0 {
              return super.tableView(tableView, cellForRowAt: indexPath)
          } else {
              let cell = tableView.dequeueReusableCell(withIdentifier: CustomCellTypeIdentifiers.YelpCommentCell, for: indexPath) as! YelpCommentCell
              cell.configureYelpCell(with: comments[indexPath.row])
              return cell
          }
      }
  </code></pre>