title: Handle JSON Response with Alamofire in Swift
body: <p>sorry for my bad english :)</p>
  
  <p>I have a problem to parse JSON response over Alamofire in Swift for an iOS app. I wrote a function to return a string of JSON response. The request and response handling I do with <a href="https://github.com/Alamofire/Alamofire" rel="nofollow">Alamofire</a> and the JSON handling I do with <a href="https://github.com/lingoer/SwiftyJSON" rel="nofollow">SwiftyJSON</a>. At the begin I declare a var called <strong>jsonString</strong> with the value <strong>test</strong>. Then I do a request to an REST Service and get a JSON response by clicking a button. This response I want to return with the function <strong>ping(url:String)</strong>. At the end I print the returned response as a test. But on the first click on the button the return value from <strong>ping</strong> is <strong>test</strong> and not the JSON string of the response. On the second click on the button I get the right return value. Why I have this problem. Is the Alamofire request an asynchronous operation? I want to wait for the response. How can I solve the problem to get the right value on first click and not <strong>test</strong> as value?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>var jsonString:String = "test"
  
  func ping(url:String) -&gt; String {
  
      Alamofire.request(.GET, url)
          .response {
              (request, response, data, error) -&gt; Void in
  
              let json = JSONValue(data as? NSData)
              self.jsonString = json.rawJSONString
      }
  
      return self.jsonString
  }
  
  @IBAction func checkOnlineStatus(sender: UIButton) {
  
      let response:String = ping("http://test.com")
  
      println(response)}
  </code></pre>