Skip to content

Instantly share code, notes, and snippets.

@rmnblm
Last active December 12, 2016 07:13
Show Gist options
  • Save rmnblm/d02bf4314f5b35aaec14ddb0fe18f1c7 to your computer and use it in GitHub Desktop.
Save rmnblm/d02bf4314f5b35aaec14ddb0fe18f1c7 to your computer and use it in GitHub Desktop.
Swift: Pyramid of Doom
/* Example JSON Object
{
"key": {
"nestedKey": {
"nestedKey": {
"nestedKey": 42.0
}
}
}
}
*/
let jsonObject = someFunctionWhichReturnsJSON()
if let dict1 = jsonObject as? [String: Any] {
if let dict2 = dict1["key"] as? [String: Any] {
if let dict3 = dict2["nestedKey"] as? [String: Any] {
if let dict4 = dict3["nestedKey"] as? [String: Any] {
if let number = dict4["nestedKey"] as? Double {
// Finally we can use the number
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment