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

Zhao Xin owenzx at gmail.com
Mon Aug 29 20:02:16 CDT 2016


I just thought updateValue(:,:) is more natural than subscript function in
Dictionary.

For example, if there is no key in the dictionary, both `dict2[key] =
value` and `dict2.updateValue(value, forKey:key)` will add a new key-value
pair. However, the subscript function does it by adding something on both
side of the equation mark, which seems not natural to me. So I prefer to
use updateValue(:,:) function instead.

Besides, updateValue(:,:) function can use potential conversions before
Swift 3.0, which makes it more convenient.
https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20160523/002021.html

Zhaoxin

On Tue, Aug 30, 2016 at 8:13 AM, Jens Alfke <jens at mooseyard.com> wrote:

>
> On Aug 29, 2016, at 4:38 PM, Zhao Xin <owenzx at gmail.com> wrote:
> ...
>
> _ = dict1.map {
>
>
> I just use a `for` loop to iterate dict1, and inside it populate dict2.
> Using `map` here seems wasteful since it’s building an array that’s thrown
> away.
>
>     dict2.updateValue(value, forKey:key)
>
>
> This is a tangent, but: Why do you use this syntax instead of the more
> readable `dict2[key] = value`? (I assume they’re equivalent internally.)
>
> —Jens
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160830/71a6613e/attachment.html>


More information about the swift-users mailing list