Reputation: 342
When I am trying to call an API in swift, how can I access the subclass?
{
"country": {
"bar": "australia",
"bar2": "melbourne"
},
}
I know with regular API calls you use jsonResult["country"] as AnyObject? as? String
however I am getting nil when doing that because I assume I am not calling "bar", just "country" which contains both "bar" and "bar2".
What is the syntax to do this? I have tried ["country"]["bar"], ["country.bar"]
etc.
Upvotes: 0
Views: 37
Reputation: 876
You may checkout https://github.com/SwiftyJSON/SwiftyJSON
Then you may use it like this:
if let bar = jsonResult["country"]["bar"].string{
// get bar now
} else {
// no result
}
BTW, if you tend to use default Swift library, you may do like this:
if let country = jsonResult["country"] as? [String: AnyObject]{
if let bar = country["bar"] as? String {
// get bar now
}
}
Upvotes: 1