Last active
February 14, 2017 21:55
-
-
Save mtrovilho/5f5de87fb03132670925 to your computer and use it in GitHub Desktop.
Swift Dictionary Extension
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
import Foundation | |
extension Dictionary { | |
func fetch(_ key: Key, defaultValue: @autoclosure () -> Value) -> Value { | |
return self[key] ?? defaultValue() | |
} | |
func fetch<T>(_ key: Key, castTo type: T.Type, defaultValue: @autoclosure () -> T) -> T { | |
return self[key] as? T ?? defaultValue() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment