title: Swift: How do I return a value within an asynchronous urlsession function?
body: <p>As you can see, I'm receiving a JSON file, parsing it using SwiftyJSON, and trying to return totalTime, but it won't let me.  How do I do this?</p>
  
  <pre><code>func googleDuration(origin: String, destination: String) -&gt; Int{
      // do calculations origin and destiantion with google distance matrix api
  
      let originFix = origin.stringByReplacingOccurrencesOfString(" ", withString: "+", options: NSStringCompareOptions.LiteralSearch, range: nil);
      let destinationFix = destination.stringByReplacingOccurrencesOfString(" ", withString: "+", options: NSStringCompareOptions.LiteralSearch, range: nil);
  
      let urlAsString = "https://maps.googleapis.com/maps/api/distancematrix/json?origins="+originFix+"&amp;destinations="+destinationFix;
      println(urlAsString);
  
      let url = NSURL(string: urlAsString)!
      let urlSession = NSURLSession.sharedSession()
  
      let task = urlSession.dataTaskWithURL(url, completionHandler: {data, response, error -&gt; Void in
          if error != nil {
              // If there is an error in the web request, print it to the console
              println(error.localizedDescription)
          }
  
          println("parsing JSON");
          let json = JSON(data: data);
          if (json["status"].stringValue == "OK") {
              if let totalTime = json["rows"][0]["elements"][0]["duration"]["value"].integerValue {
                  println(totalTime);
              }
          }
      })
      task.resume();
  }
  </code></pre>