title: Why do I get an error Code=3840 Parsing JSON?
body: <p>I'm new to Swift and iOS development in general. I'm testing and learning how to use remote server api's. I'm getting JSON from a Strongloop (Loopback) api while attempting authentication and the parsing I'm trying to use gets an error: </p>
  
  <blockquote>
    <p>Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (No value.) UserInfo=0x7fd623e38870 {NSDebugDescription=No value.}</p>
  </blockquote>
  
  <p>Here is the return value in a string, I'm obviously getting a proper JSON response from Loopback, an authentication token, ttl, date and userId: </p>
  
  <blockquote>
    <p>{"id":"BHzKnjrbUPn9KSC1GegQTNHJFNwfuifNXPfZuKkYxC5IwRDEHuerurvSdBMzzrVi","ttl":1209600,"created":"2015-07-03T13:04:39.791Z","userId":2}</p>
  </blockquote>
  
  <p>I think the actual problem is NOT occurring in the parseJSON method but rather the performLoginRequestWithURL method. It's returning an empty string. Something to do with the asynchronous request. I noticed that the json string variable does not get set until after it's returned from the method, so it gets returned blank. If I put two println(json) methods, one inside the asynchronous request and one after it, the one after prints first. Which makes sense in a way but I don't know how to solve this. I need to get the json returned from the post but I don't know how to capture that. Something tells me I need to use a synchronous request instead but I don't know how to do that in this context of getting json from a POST request.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>//Login button pressed
  @IBAction func login() {
      //Gets url string
      let url = getLogin()
      //Posts url with UITextField data.
      if let jsonString = performLoginRequestWithURL(url) {
          //Error occurs in the parseJSON method
          if let dictionary = parseJSON(jsonString) {
              /*
              if let at = dictionary["id"] as? String {
                  accesstoken = at
              }
              if let id = dictionary["userId"] as? Int {
                  println(id)
              }
              */
          }
      }
  
  }
  
  func getLogin() -&gt; NSURL {
      let toEscape = "http://localhost:3000/api/Users/login"
      let urlString = toEscape.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
      let url = NSURL(string: urlString)
      return url!
  }
  
  func performLoginRequestWithURL(url: NSURL) -&gt; String? {
      let bodyData = "username=\(textEmail.text)&amp;password=\(textPW.text)"
      var request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
      var json = ""
      request.HTTPMethod = "POST"
      request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding)
      NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()){
          response, data, error in
  
          if data != nil {
              json = NSString(data: data, encoding: NSUTF8StringEncoding) as! String
          }
          println(json)
      }
      return json
  }
  
  func parseJSON(jsonString: String) -&gt; [String: AnyObject]? {
      if let data = jsonString.dataUsingEncoding(NSUTF8StringEncoding) {
          var error: NSError?
          if let json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: &amp;error) as? [String: AnyObject] {
              return json
          } else if let error = error {
              //Here's where the error comes back.
              println("JSON Error: \(error)")
          } else {
              println("Unknown JSON Error")
          }
      }
      return nil
  }
  </code></pre>