mankee mankee - 7 months ago 58
Swift Question

Extracting values from facebook json response

I have the following JSON response from FB:

result {
"age_range" = {
min = 21;
email = "";
gender = male;
id = xxxxxxxxx;
name = "a b";

I extract each of the elements using

let name: String? = result["name"] as? String

This works fine, however I don't know how to extract the
fields from the
. I tried using
let min: String? = (result.valueForKey("age_range")).valueForKey("min")
however this returns a null pointer exception.

enter image description here


Forget those silly type annotations. They do more harm than good.

And never use valueForKey to parse JSON, just use key subscription.

From the FB documentation min is clearly an integer.

if let ageRange = result["age_range"] as? [String: Int], min = ageRange["min"] {