mankee mankee - 6 months ago 47
Swift Question

Extracting values from facebook json response

I have the following JSON response from FB:

result {
"age_range" = {
min = 21;
};
email = "ddsdasddsadas@gmail.com";
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
min
and
max
fields from the
age_range
. I tried using
let min: String? = (result.valueForKey("age_range")).valueForKey("min")
however this returns a null pointer exception.

enter image description here

Answer

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"] {
    print(min)
}
Comments