Skip to content

Instantly share code, notes, and snippets.

@JosephDuffy
Last active March 22, 2017 16:52
Show Gist options
  • Save JosephDuffy/31b462439357cc1716ed3eac3b98a6bb to your computer and use it in GitHub Desktop.
Save JosephDuffy/31b462439357cc1716ed3eac3b98a6bb to your computer and use it in GitHub Desktop.
import Foundation
extension Dictionary {
subscript(keys: String...) -> Any? {
var value: Any = self
for key in keys {
guard let dictionary = value as? [String: Any] else { return nil }
guard let newValue = dictionary[key] else { return nil }
value = newValue
}
return value
}
}
let dict = [
"foo": [
"bar": [
"value": 1
]
]
]
dict["foo", "bar", "value"] // 1
dict["foo", "bar", "😱"] // nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment