LoginSignup
1
1

More than 5 years have passed since last update.

Swift の Dictionary に map を実装する

Posted at

Swift の Dictionary に map を実装してみた。
Ruby の Hash#map のイメージ。

extension Dictionary {

    func map<T>(transform: (Key, Value) -> T) -> [T] {
        return Swift.map(self, transform)
    }
}


var dict = [String: String]()
dict["hello"] = "world"

let array = dict.map { (k: String, v: String) -> String in
    return v
}

println(array)  // ["world"]
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1