Created
April 12, 2019 14:12
-
-
Save radianttap/dccb96ade1fd66e93626bfaa97f2b980 to your computer and use it in GitHub Desktop.
Dictionary, mapKeys (counterpart to mapValues)
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
public extension Dictionary { | |
func mapKeys<T>(_ transform: (Key) throws -> T) rethrows -> [T : Value] where T: Hashable { | |
var dict: [T: Value] = [:] | |
for (key, value) in self { | |
let newKey = try transform(key) | |
dict[newKey] = value | |
} | |
return dict | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment