Created
October 31, 2014 13:49
-
-
Save olgakogan/bd6e5eff98aeda63c68c to your computer and use it in GitHub Desktop.
Swift - get from dictionary, with default value
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
extension Dictionary { | |
func get(key: Key, defaultValue: Value) -> Value { | |
/** | |
Returns the value for the given key (if exists), otherwise returns the default value. | |
*/ | |
if let value = self[key] { | |
return value | |
} else { | |
return defaultValue | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems
dict[key, default: value]
is what you need.Here is the API reference.