[swift-users] Mapping a Dictionary to a Dictionary?

Daniel Tartaglia danielt1263 at gmail.com
Wed Aug 31 12:51:03 CDT 2016


I use the below to do this:

dict2 = dict1.map { $0.withUpdate($0.0, value: $0.1) }

The above will effectively do a straight copy, you will probably want to modify the values passed in though…

https://gist.github.com/dtartaglia/6141e6003e408399c2dd7a7cc266dce6#file-dictionaryextensions-swift

extension Dictionary {

    /// An immutable version of update. Returns a new dictionary containing self's values and the key/value passed in.
    func withUpdate(key: Key, value: Value) -> Dictionary<Key, Value> {
        var result = self
        result[key] = value
        return result
    }
}


> On Aug 30, 2016, at 1:00 PM, swift-users-request at swift.org wrote:
> 
> [swift-users] Mapping a Dictionary to a Dictionary?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160831/dbfd049e/attachment.html>


More information about the swift-users mailing list