Skip to content

Instantly share code, notes, and snippets.

@mtrovilho
Last active February 14, 2017 21:55
Show Gist options
  • Save mtrovilho/5f5de87fb03132670925 to your computer and use it in GitHub Desktop.
Save mtrovilho/5f5de87fb03132670925 to your computer and use it in GitHub Desktop.
Swift Dictionary Extension
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