10

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!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Koesh
  • 431
  • 5
  • 12
  • I also have this issue. How on Earth do i assign the JSON Dictionary values to a string variable? I have a var `var serverID:String?` and when trying `self.serverID = responseObject["server_id"] as? String` it crashes Xcode – Sean Jun 21 '14 at 03:25
  • I had success with this approach: http://stackoverflow.com/questions/24259756/unable-to-parse-json-from-afnetworkings-responseobject – heycarsten Jul 03 '14 at 15:50
  • @heycarsten Unfortunately I get EXC_BAD_INSTRUCTION on Simulator and EXC_BREAKPOINT on Device with that code. :( – Koesh Jul 10 '14 at 18:19
  • @Sean It seems automatically convert to NUMBER TYPE, not String!!! I had same issue, and I try to find a way to convert as String. – Allen Heavey Sep 10 '14 at 16:07
  • hi any solution for this? – Jan Apr 12 '19 at 10:53

3 Answers3

1

try this

if let responseArray = response as? NSArray {
    let firstElement = responseArray[0]
    // do something with the first element
}
greg
  • 31
  • 2
0

I think your problem lays within sending it back in the successBlock. Since the retrieved information ain't presented properly in the description object.

var jsonArrayDictionary = response.result.value as? [[String: Any]]

for item in jsonArrayDictionary {
     dump(item["_id"] as? String)
     dump(item["name"] as? String)
}

That should probably do it.

Vollan
  • 1,887
  • 11
  • 26
-5

Your example response is not valid JSON.

If your example is an Array, JSON would have square brackets instead of parens, field names in quotes and colons instead of equals signs. For example:

[
  {
    "_id": 1234,
    "name": "foo bar"
  },
  {
    "_id": 12122,
    "name": "baz"
  }
]

Also see one of the JSON linters, e.g.: jsonlint.com

Steve D.
  • 150
  • 1
  • 5