title: NSURLSession Response String completion block - Swift
body: <p>I want to wait for a responseString to complete before calling the next function "nextScreen()" (segue). At the moment I have an if statement to make sure it is not nil before proceeding, but sometimes the the next function/segue is called because the responseString is still downloading. </p>
  
  <p>Could you help with a completion block? I have found completion blocks for NSURLSession, but these just wait for the initial HTTP call to complete, not the response string.</p>
  
  <pre><code>func getProfiles(){
      func post(completion: (message: String?) -&gt; Void) {
          let request = NSMutableURLRequest(URL: NSURL(string: "http://**.**.**.**/EPG/XML/QueryProfile")!)
          request.HTTPMethod = "POST"
          let postString = "&lt;QueryProfileReq&gt;&lt;type&gt;1&lt;/type&gt;&lt;/QueryProfileReq&gt;"
          request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
  
          let task: Void = NSURLSession.sharedSession().dataTaskWithRequest(request,
              completionHandler: {(data: NSData!,
                  response: NSURLResponse!,
                  error: NSError!) in
                  if error != nil {
                      println("error=\(error)")
                      let alert = UIAlertView()
                      alert.delegate = self
                      alert.title = "Login Error"
                      alert.message = "\(error)"
                      alert.addButtonWithTitle("OK")
                      alert.show()
                      self.view.endEditing(true)
                      return
                  }
                  if let responseString = NSString(data: data, encoding: NSUTF8StringEncoding) {
                      if response != nil {
                          println("got profiles")
                          self.nextScreen()
                      }
                      self.dataVar = data // UPDATES VARIABLE TO SEND
                  }
          }).resume()
  
      }
  }
  </code></pre>