Last active
December 12, 2016 07:13
-
-
Save rmnblm/d02bf4314f5b35aaec14ddb0fe18f1c7 to your computer and use it in GitHub Desktop.
Swift: Pyramid of Doom
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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