So, I am using AFNetworking 2.0 (ObjC framework with Bridging-Header) to make some requests on a local server. I have followed a few tutorials to code it using Swift. This is the code:
var success = { (operation:AFHTTPRequestOperation!, response:AnyObject!) -> Void in
println(response.description)
successBlock(result:response.description)
}
var failure = { (operation:AFHTTPRequestOperation!, response:NSError!) -> Void in
println(response.description)
errorBlock(error:response.description)
}
var manager = AFHTTPRequestOperationManager()
manager.responseSerializer = AFJSONResponseSerializer();
manager.GET("http://127.0.0.1:8080/api/manufacturer", parameters: nil, success: success, failure: failure)
It retrieves the json and prints it successfully. The response is something like this:
(
{
"_id" = 539f0973e3c7f4ab1f6078f5;
name = Manufacturer01;
},
{
"_id" = 539f18c5e3c7f4ab1f6078f6;
name = Manufacturer02;
}
)
However, I am unable to parse it... I tried response[0]
to get the first element, but it crashes the simulator and even Xcode6 when I try to do: (lldb) > po response[0]
. I tried everything, every example I have seen explains how to print the result but nothing about parsing each field.
The response
object looks like this when I try to debug it:
value = Some {
Some = (instance_type = Builtin.RawPointer = 0x0b240710 -> 0x00bc5da0 (void *)0x00bc5db4: __NSCFArray)
}
Any clue? Thanks in advance!
try this
if let responseArray = response as? NSArray {
let firstElement = responseArray[0]
// do something with the first element
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With