Created
March 4, 2017 13:02
-
-
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
This file contains 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
// 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