title: How do I access the data when I am using NSURLSession?
body: <p>I am new to iOS development. I am using Swift and I have very little experience with Objective-C, so some of the other possibly related answers are tricky to understand. I am trying to understand how to use <code>NSURLSession</code> to get some data from a JSON file on the Web. I found some useful information about getting a file from a URL, but like this other StackOverflow user (<a href="https://stackoverflow.com/questions/22609434/nsurlsessiondatatask-datataskwithurl-completion-handler-not-getting-called">NSURLSessionDataTask dataTaskWithURL completion handler not getting called</a>), I heard that <code>NSURLConnection</code> was not the current way to get data, so I'm trying to use <code>NSURLSession</code>.</p>
  
  <p>When I am getting my JSON from the bundle, I am using this extension to
  Dictionary (I am pretty sure I got this code from a tutorial):</p>
  
  <pre><code>static func loadJSONFromBundle(filename: String) -&gt; Dictionary&lt;String, AnyObject&gt;? {
      let path = NSBundle.mainBundle().pathForResource(filename, ofType: ".json")
      if !path {
          println("Could not find level file: \(filename)")
          return nil
      }
      var error: NSError?
      let data: NSData? = NSData(contentsOfFile: path, options: NSDataReadingOptions(),
          error: &amp;error)
      if !data {
          println("Could not load level file: \(filename), error: \(error!)")
          return nil
      }
      let dictionary: AnyObject! = NSJSONSerialization.JSONObjectWithData(data,
          options: NSJSONReadingOptions(), error: &amp;error)
      if !dictionary {
          println("Level file '\(filename)' is not valid JSON: \(error!)")
          return nil
      }
      return dictionary as? Dictionary&lt;String, AnyObject&gt;
  }
  </code></pre>
  
  <p>I'd like to do something similar for getting a dictionary from a JSON file that is on the web because I don't anticipate wanting to include all of my JSON files in the bundle. So far, I have this:</p>
  
  <pre><code>static func loadJSONFromWeb(urlstring: String) -&gt; Dictionary&lt;String, AnyObject&gt;? {
  
      let url = NSURL(string: urlstring)
      let config = NSURLSessionConfiguration.defaultSessionConfiguration()
      let session = NSURLSession(configuration: config, delegate: nil, delegateQueue: NSOperationQueue())
  
      var error: NSError?
      //I think I am using the completionHandler incorrectly. I'd like to access the data from the download
      let task = session.downloadTaskWithRequest(NSURLRequest(URL: url), {(url, response, error) in println("The response is: \(response)")
          })
      task.resume()
  
      //Isn't this contentsOfURL thing supposed to go with the connection stuff rather than the session stuff?
      //How can I do this with a session? How can I create and use a completionHandler? This way seems clunky.
      let data: NSData? = NSData(contentsOfURL: url)
      if !data {
          println("Could not load data from file: \(url), error: \(error!)")
          return nil
      }
      println("The data is: \(data)")
  
      let dictionary: AnyObject! = NSJSONSerialization.JSONObjectWithData(data,
          options: NSJSONReadingOptions(), error: &amp;error)
      if !dictionary {
          println("The file at '\(url)' is not valid JSON, error: \(error!)")
          return nil
      }
  
      return dictionary as? Dictionary&lt;String, AnyObject&gt;   
  }
  </code></pre>
  
  <p>I think that my actual question that most needs answering is this: <strong>Where
  is the data?</strong> I don't think I am using sessions and tasks correctly. I feel like I'm
  starting a session to connect to a specific URL and using <code>resume()</code> to
  start the download task I want to make happen, but I don't know how to
  get the data from that JSON file.</p>
  
  <p>If I need to use a <code>completionHandler</code> and a request in a way similar to what I found here:
  (<a href="https://stackoverflow.com/questions/24798351/popviewcontrolleranimated-work-slow-inside-nsurlsessiondatatask/24800060#24800060">popViewControllerAnimated work slow inside NSURLSessionDataTask</a>) can someone please explain how the 'data' in the <code>completionHandler</code> relates to the data in the fie I am trying to read/download? I am a bit baffled by the <code>completionHandler</code> and how to use it properly.</p>
  
  <p>I looked at the documentation for <code>NSData</code> as well, but I didn't see anything that helped me understand how to get data from my session (or how to initialize an instance of <code>NSData</code> given my session). As far as I can tell form the documentation for <code>NSURLDownloadTask</code>, the task itself is not how I can access the data. It looks like the data comes from the session and task through the <code>completionHandler</code>.</p>
  
  <hr>
  
  <p>EDIT:
  I also looked at the documentation for <code>NSURLSessionDownloadDelegate</code>, but I could really use an example in Swift with some explanation about how to use the delegate. This led me to the URL Loading System Programming Guide. I'm guessing the benefits of using a session must be huge if the documentation is this complicated. I'm going to keep looking for more information on the URL Loading System.</p>
  
  <p>I found this answer helpful (but I'm so new I can't upvote anything yet): <a href="https://stackoverflow.com/a/22177659/3842582">https://stackoverflow.com/a/22177659/3842582</a> It helped me see that I am probably going to need to learn to use a delegate.</p>
  
  <p>I also looked at the URL Loading System Programming Guide. I think what I really need is help with a completionHandler. How can I get my data? Or, am I already doing it correctly using <code>NSData(contentsOfURL: url)</code> (because I don't think I am).</p>
  
  <hr>
  
  <p>Thank you in advance for any help you can offer!</p>