Skip to content

Instantly share code, notes, and snippets.

@Otbivnoe
Last active October 4, 2018 19:22
Show Gist options
  • Save Otbivnoe/e585692c2f9c38544739a5a8c1e9360a to your computer and use it in GitHub Desktop.
Save Otbivnoe/e585692c2f9c38544739a5a8c1e9360a to your computer and use it in GitHub Desktop.
@dynamicMemberLookup
protocol DictionaryDynamicLookup {
associatedtype Key
associatedtype Value
subscript(key: Key) -> Value? { get }
}
extension DictionaryDynamicLookup where Key == String {
subscript(dynamicMember member: String) -> Value? {
return self[member]
}
}
extension Dictionary: DictionaryDynamicLookup {}
let dict: [String : Any] = [
"name": "Nikita",
"age": 24
]
print(dict.name)
print(dict.age)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment