Skip to content

Instantly share code, notes, and snippets.

@ranmyfriend
Created March 4, 2017 13:02
Show Gist options
  • Save ranmyfriend/00f8eee86f2cda63ce7284d54965187b to your computer and use it in GitHub Desktop.
Save ranmyfriend/00f8eee86f2cda63ce7284d54965187b to your computer and use it in GitHub Desktop.
We create a special subscript for nested `[String:Any]` dictionaries that also has a getter
// Created - Ranjit
import Foundation
//: ### Subscript Solution
//: We create a special subscript for nested `[String:Any]` dictionaries that also has a getter:
extension Dictionary {
subscript(jsonDict key: Key) -> [String:Any]? {
get {
return self[key] as? [String:Any]
}
}
subscript(string key: Key) -> String? {
get {
return self[key] as? String
}
}
subscript(int key: Key) -> Int? {
get {
return self[key] as? Int
}
}
subscript(bool key: Key) -> Bool? {
get {
return self[key] as? Bool
}
}
subscript(double key: Key) -> Double? {
get {
return self[key] as? Double
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment