title: Passing data back to the main class variable from POST Request Swift
body: <p>I am new to swift and totally lost in the order of functions and the handle of task.resume for the HTTP Post Requests.</p>
  
  <p>So I have a variable called "siteList" in my class. 
  I am trying to then run a function to populate the JSON data into the siteList array. The JSON data is coming from a POST REQUEST. </p>
  
  <p>The problem I am having is that:</p>
  
  <ol>
  <li>I can't access the siteList within URLSession Request. It is requiring me to assign it as self.siteList, which does not pass the value back to the siteList at class level. </li>
  <li>I could use the  siteData variable outside the function as it is declared at swift file level, but the problem is I can't seem to get the function order right. In other words, when I tried to print siteData after task.resume(), it still returns empty.</li>
  </ol>
  
  <p>I tried breaking this into two functions, one to get process POST request and get siteData, another one to pass the siteData to the siteList variable, and use completion handler. But the problem is then the 2nd function is required to be a self.functionName again, which basically makes the value in accessible at class level..</p>
  
  <p>I am sure I am probably missing something here but I am just completely lost. I've spent hours on this and can't seem to find a solution... </p>
  
  <p>Any help would be appreciated..</p>
  
  <p>The code looks like this:</p>
  
  <pre><code>var siteData = NSArray()
  
  class SiteListManager: NSObject {
      var siteList=[Site]()    
      let category = GlobalConstants.kCat
      let keyword = GlobalConstants.kKeyword
      let latitude = GlobalConstants.kLat
      let longitude = GlobalConstants.kLong
  
      // MARK: - init
      override init() {
          super.init()        
          getSiteList(category: category, keyword:keyword, latitude:latitude, longitude:longitude)
      }
  
      func getSiteList(category: String, keyword: String, latitude: String, longitude: String) {
  
          let myUrl = URL(string: "http://www.goseesa.org.php7-34.ord1-1.websitetestlink.com/sites/list");
          var request = URLRequest(url:myUrl!)
          request.httpMethod = "POST"// Compose a query string
  
          let postString = "keyword="+category+"&amp;cid="+keyword+"&amp;lat="+latitude+"&amp;long="+longitude
          request.httpBody = postString.data(using: String.Encoding.utf8);
  
          let task = URLSession.shared.dataTask(with: request, completionHandler: { (data: Data?, response: URLResponse?, error: Error?) in
              do {
                  siteData = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! NSArray
  
                  let kTotalSites = siteData.count
                  self.siteList = [Site]()
                  // Now we can access value of First Name by its key
                  //let siteList = parseJSON["user"] as? NSDictionary
                  for i in 0..&lt;kTotalSites {
                      let siteContent = siteData[i] as! NSDictionary
                      let name = siteContent.value(forKey: "name") as! String
  
                      //load site information
                      let site = Site(name: name)
                      self.siteList.append(site)
                  }//end of compiling site list
               }catch {
                      print(error)
               }
          })
          task.resume()
      }
  }
  </code></pre>