<div dir="ltr">For example, you might want to merge values:<div><br></div><div>







<p class="m_154567971810913444gmail-p1"><span class="m_154567971810913444gmail-s1">let</span><span class="m_154567971810913444gmail-s2"> d1 = [</span><span class="m_154567971810913444gmail-s3">&quot;Apples&quot;</span><span class="m_154567971810913444gmail-s2">: </span><span class="m_154567971810913444gmail-s4">20</span><span class="m_154567971810913444gmail-s2">, </span><span class="m_154567971810913444gmail-s3">&quot;Oranges&quot;</span><span class="m_154567971810913444gmail-s2">: </span><span class="m_154567971810913444gmail-s4">13</span><span class="m_154567971810913444gmail-s2">]</span></p>
<p class="m_154567971810913444gmail-p1"><span class="m_154567971810913444gmail-s1">let</span><span class="m_154567971810913444gmail-s2"> d2 = [</span><span class="m_154567971810913444gmail-s3">&quot;Oranges&quot;</span><span class="m_154567971810913444gmail-s2">: </span><span class="m_154567971810913444gmail-s4">3</span><span class="m_154567971810913444gmail-s2">, </span><span class="m_154567971810913444gmail-s3">&quot;Cherries&quot;</span><span class="m_154567971810913444gmail-s2">: </span><span class="m_154567971810913444gmail-s4">9</span><span class="m_154567971810913444gmail-s2">]</span></p><p class="m_154567971810913444gmail-p1"><span class="m_154567971810913444gmail-s2"><br></span></p>
<p class="m_154567971810913444gmail-p3"><span class="m_154567971810913444gmail-s1">let</span><span class="m_154567971810913444gmail-s3"> d3 = [</span><span class="m_154567971810913444gmail-s5">d1</span><span class="m_154567971810913444gmail-s3">, </span><span class="m_154567971810913444gmail-s5">d2</span><span class="m_154567971810913444gmail-s3">].</span><span class="m_154567971810913444gmail-s1">reduce</span><span class="m_154567971810913444gmail-s3">([</span><span class="m_154567971810913444gmail-s6">String</span><span class="m_154567971810913444gmail-s3"> : </span><span class="m_154567971810913444gmail-s6">Int</span><span class="m_154567971810913444gmail-s3">]()) { acc, d </span><span class="m_154567971810913444gmail-s1">in</span></p>
<p class="m_154567971810913444gmail-p3"><span class="m_154567971810913444gmail-s3">  </span><span class="m_154567971810913444gmail-s1">var</span><span class="m_154567971810913444gmail-s3"> dx = acc</span></p>
<p class="m_154567971810913444gmail-p3"><span class="m_154567971810913444gmail-s3">  </span><span class="m_154567971810913444gmail-s1">for</span><span class="m_154567971810913444gmail-s3"> entry </span><span class="m_154567971810913444gmail-s1">in</span><span class="m_154567971810913444gmail-s3"> d {</span></p>
<p class="m_154567971810913444gmail-p3"><span class="m_154567971810913444gmail-s3">    </span><span class="m_154567971810913444gmail-s1">let</span><span class="m_154567971810913444gmail-s3"> key = entry.key</span></p>
<p class="m_154567971810913444gmail-p3"><span class="m_154567971810913444gmail-s3">    </span><span class="m_154567971810913444gmail-s1">guard</span><span class="m_154567971810913444gmail-s3"> dx[key] == </span><span class="m_154567971810913444gmail-s1">nil</span><span class="m_154567971810913444gmail-s3"> </span><span class="m_154567971810913444gmail-s1">else</span><span class="m_154567971810913444gmail-s3"> {</span></p>
<p class="m_154567971810913444gmail-p3"><span class="m_154567971810913444gmail-s3">      dx[key]! += entry.value</span></p>
<p class="m_154567971810913444gmail-p4"><span class="m_154567971810913444gmail-s2">      </span><span class="m_154567971810913444gmail-s3">continue</span></p>
<p class="m_154567971810913444gmail-p3"><span class="m_154567971810913444gmail-s3">    }</span></p>
<p class="m_154567971810913444gmail-p3"><span class="m_154567971810913444gmail-s3">    dx[entry.key] = entry.value</span></p>
<p class="m_154567971810913444gmail-p3"><span class="m_154567971810913444gmail-s3">  }</span></p>
<p class="m_154567971810913444gmail-p4"><span class="m_154567971810913444gmail-s2">  </span><span class="m_154567971810913444gmail-s3">return</span><span class="m_154567971810913444gmail-s2"> dx</span></p><p class="m_154567971810913444gmail-p4">}</p>
<p class="m_154567971810913444gmail-p2"><span class="m_154567971810913444gmail-s3"></span><br></p>
<p class="m_154567971810913444gmail-p5"><span class="m_154567971810913444gmail-s1">print</span><span class="m_154567971810913444gmail-s2">(</span><span class="m_154567971810913444gmail-s5">d3</span><span class="m_154567971810913444gmail-s2">) </span><span class="m_154567971810913444gmail-s3">// [&quot;Cherries&quot;: 9, &quot;Apples&quot;: 20, &quot;Oranges&quot;: 16]</span></p></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Nov 11, 2016 at 9:14 AM, Ray Fix via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Hi Mr Bee,</div><div><br></div><div>The reason I don’t think it is provided is because it is difficult to know what to do when keys collide.  You could easily write such a thing and decide your own policy.  For example:</div><div><br></div><div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(201,27,19)"><span style="font-variant-ligatures:no-common-ligatures;color:#c32275">let</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> d1 = [</span><span style="font-variant-ligatures:no-common-ligatures">&quot;Apples&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">: </span><span style="font-variant-ligatures:no-common-ligatures;color:#0435ff">20</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">, </span><span style="font-variant-ligatures:no-common-ligatures">&quot;Oranges&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">: </span><span style="font-variant-ligatures:no-common-ligatures;color:#0435ff">13</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">]</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(201,27,19)"><span style="font-variant-ligatures:no-common-ligatures;color:#c32275">let</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> d2 = [</span><span style="font-variant-ligatures:no-common-ligatures">&quot;Oranges&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">: </span><span style="font-variant-ligatures:no-common-ligatures;color:#0435ff">3</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">, </span><span style="font-variant-ligatures:no-common-ligatures">&quot;Cherries&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">: </span><span style="font-variant-ligatures:no-common-ligatures;color:#0435ff">9</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">]</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="font-variant-ligatures:no-common-ligatures;color:#c32275">extension</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> </span><span style="font-variant-ligatures:no-common-ligatures">Dictionary</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> {</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#c32275">func</span><span style="font-variant-ligatures:no-common-ligatures"> merged(with another: [</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Key</span><span style="font-variant-ligatures:no-common-ligatures">: </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Value</span><span style="font-variant-ligatures:no-common-ligatures">]) -&gt; [</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Key</span><span style="font-variant-ligatures:no-common-ligatures">: </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Value</span><span style="font-variant-ligatures:no-common-ligatures">] {</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#c32275">var</span><span style="font-variant-ligatures:no-common-ligatures"> result = </span><span style="font-variant-ligatures:no-common-ligatures;color:#c32275">self</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#c32275">for</span><span style="font-variant-ligatures:no-common-ligatures"> entry </span><span style="font-variant-ligatures:no-common-ligatures;color:#c32275">in</span><span style="font-variant-ligatures:no-common-ligatures"> another {</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">            result[entry.key] = entry.value</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#c32275">return</span><span style="font-variant-ligatures:no-common-ligatures"> result</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#c32275">let</span><span style="font-variant-ligatures:no-common-ligatures"> result = </span><span style="font-variant-ligatures:no-common-ligatures;color:#539aa4">d1</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#294c50">merged</span><span style="font-variant-ligatures:no-common-ligatures">(with: </span><span style="font-variant-ligatures:no-common-ligatures;color:#539aa4">d2</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div></div><div><br></div><br><div><blockquote type="cite"><div><div class="h5"><div>On Nov 11, 2016, at 12:05 AM, Mr Bee via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br class="m_2243869007652622634Apple-interchange-newline"></div></div><div><div><div class="h5"><div><div style="background-color:rgb(255,255,255);font-family:HelveticaNeue-Light,&#39;Helvetica Neue Light&#39;,&#39;Helvetica Neue&#39;,Helvetica,Arial,&#39;Lucida Grande&#39;,sans-serif;font-size:16px"><div id="m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9177"><span>Hi,</span></div><div id="m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9177"><span><br></span></div><div id="m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9177" dir="ltr"><span id="m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9300">I&#39;m using Swift v3 on an El Capitan machine. I want to merge a dictionary into another compatible dictionary. However, I couldn&#39;t find addEntries function in the dictionary instance, like it was on NSMutableDictionary (<a href="https://developer.apple.com/reference/foundation/nsmutabledictionary" target="_blank">https://developer.apple.com/<wbr>reference/foundation/<wbr>nsmutabledictionary</a>). </span></div><div id="m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9177" dir="ltr"><span><br></span></div><div id="m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9177" dir="ltr"><span id="m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9391">Does that mean that Swift standard library won&#39;t provide such similar function for native Swift dictionary? Or is there any other way of doing that natively? I mean using the built-in Swift&#39;s native dictionary function (<a href="https://developer.apple.com/reference/swift/dictionary" target="_blank">https://developer.apple.com/<wbr>reference/swift/dictionary</a>), no need to write a custom function, or bridging to NSMutableDictionary.</span></div><div id="m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9177" dir="ltr"><span><br></span></div><div id="m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9177" dir="ltr"><span>Thank you.</span></div><div id="m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9177" dir="ltr"><span><br></span></div><div id="m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9177" dir="ltr"><span>Regards,</span></div><div class="m_2243869007652622634signature" id="m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9174"><div id="m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9175"><br></div>–Mr Bee<div id="m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9301"><br></div></div></div></div></div></div>______________________________<wbr>_________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>