mark mark - 2 years ago 286
Swift Question

Printing data in Alamofire request

I am trying to print the data returned from an alamofire post request in swift like so:

Alamofire.request(.POST, "", parameters: ["operand1": "123"]).response { request, response, data, error in
print(data, radix: 16)

but when I try to print data it doesn't print a string a string, instead it prints:

I am a bit new to swift so I don't really know if there is a way or a reason for this.

Answer Source

That is the raw server response which is in NSData type. You can make use of some built-in serializers such as .responseString() in order to convert that into user readable value:

Alamofire.request(.POST, "", parameters: ["operand1": "123"]).responseString { response in
  print("Response String: \(response.result.value)")

You can check out other serializers in the Alamofire documentation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download