[swift-users] How to merge two dictionaries?

Jens Alfke jens at mooseyard.com
Fri Nov 11 11:47:31 CST 2016

> On Nov 11, 2016, at 12:14 AM, Ray Fix via swift-users <swift-users at swift.org> wrote:
> The reason I don’t think it is provided is because it is difficult to know what to do when keys collide.

When you add a single entry to a dictionary with []=, it simply replaces any existing value. I would expect adding multiple entries to do the same. (Which is what NSDictionary’s -addEntries: method does.)

Sure, there are different policies you could want, like ignoring duplicate keys or somehow combining the values, but replacing is a common default. It seems wrong not to have this method just because there’s more than one way it could work. (“Don’t let the best be the enemy of the good.”)

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

More information about the swift-users mailing list