Skip to content

Instantly share code, notes, and snippets.

View halftrue's full-sized avatar
🐟
Busy

叉叉咖 halftrue

🐟
Busy
View GitHub Profile
@halftrue
halftrue / Dictionary+KeysForValue.swift
Created April 29, 2016 09:51 — forked from ijoshsmith/Dictionary+KeysForValue.swift
Find keys mapped to a value in Swift dictionary
extension Dictionary where Value: Equatable {
/// Returns all keys mapped to the specified value.
/// ```
/// let dict = ["A": 1, "B": 2, "C": 3]
/// let keys = dict.keysForValue(2)
/// assert(keys == ["B"])
/// assert(dict["B"] == 2)
/// ```
func keysForValue(value: Value) -> [Key] {
return flatMap { (key: Key, val: Value) -> Key? in