<html><head></head><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue-Light, Helvetica Neue Light, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px"><div id="yui_3_16_0_ym19_1_1478858311546_10038"><span id="yui_3_16_0_ym19_1_1478858311546_10037">Thank you for your answer, but as I've said, I'm looking for a ready to use solution right from the Dictionary data type. Not using custom function or extension, neither bridging with NSMutableDictionary.</span></div><div></div><div id="yui_3_16_0_ym19_1_1478858311546_10039"> </div><div id="yui_3_16_0_ym19_1_1478858311546_10039">Actually, as I'm now learning Swift, I write my learning process into a Swift blog (but in my local language). I was surprised to find that dictionary doesn't have method for this simple operation. I was expecting it would be as easy as using + operator like in array data type.</div><div id="yui_3_16_0_ym19_1_1478858311546_10039"><br></div><div class="signature" id="yui_3_16_0_ym19_1_1478858311546_10041"><div id="yui_3_16_0_ym19_1_1478858311546_10040"><br></div>–Mr Bee<div id="yui_3_16_0_ym19_1_1478858311546_10297"><br></div></div> <div class="qtdSeparateBR"><br><br></div><div class="yahoo_quoted" style="display: block;"> <div style="font-family: HelveticaNeue-Light, Helvetica Neue Light, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;"> <div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;"> <div dir="ltr"><font size="2" face="Arial"> Pada Jumat, 11 November 2016 16:31, Michael Nisi <michael.nisi@gmail.com> menulis:<br></font></div> <br><br> <div class="y_msg_container"><div id="yiv0956377843"><div><div dir="ltr">For example, you might want to merge values:<div><br clear="none"></div><div>
<div class="yiv0956377843m_154567971810913444gmail-p1"><span class="yiv0956377843m_154567971810913444gmail-s1">let</span><span class="yiv0956377843m_154567971810913444gmail-s2"> d1 = [</span><span class="yiv0956377843m_154567971810913444gmail-s3">"Apples"</span><span class="yiv0956377843m_154567971810913444gmail-s2">: </span><span class="yiv0956377843m_154567971810913444gmail-s4">20</span><span class="yiv0956377843m_154567971810913444gmail-s2">, </span><span class="yiv0956377843m_154567971810913444gmail-s3">"Oranges"</span><span class="yiv0956377843m_154567971810913444gmail-s2">: </span><span class="yiv0956377843m_154567971810913444gmail-s4">13</span><span class="yiv0956377843m_154567971810913444gmail-s2">]</span></div>
<div class="yiv0956377843m_154567971810913444gmail-p1"><span class="yiv0956377843m_154567971810913444gmail-s1">let</span><span class="yiv0956377843m_154567971810913444gmail-s2"> d2 = [</span><span class="yiv0956377843m_154567971810913444gmail-s3">"Oranges"</span><span class="yiv0956377843m_154567971810913444gmail-s2">: </span><span class="yiv0956377843m_154567971810913444gmail-s4">3</span><span class="yiv0956377843m_154567971810913444gmail-s2">, </span><span class="yiv0956377843m_154567971810913444gmail-s3">"Cherries"</span><span class="yiv0956377843m_154567971810913444gmail-s2">: </span><span class="yiv0956377843m_154567971810913444gmail-s4">9</span><span class="yiv0956377843m_154567971810913444gmail-s2">]</span></div><div class="yiv0956377843m_154567971810913444gmail-p1"><span class="yiv0956377843m_154567971810913444gmail-s2"><br clear="none"></span></div>
<div class="yiv0956377843m_154567971810913444gmail-p3"><span class="yiv0956377843m_154567971810913444gmail-s1">let</span><span class="yiv0956377843m_154567971810913444gmail-s3"> d3 = [</span><span class="yiv0956377843m_154567971810913444gmail-s5">d1</span><span class="yiv0956377843m_154567971810913444gmail-s3">, </span><span class="yiv0956377843m_154567971810913444gmail-s5">d2</span><span class="yiv0956377843m_154567971810913444gmail-s3">].</span><span class="yiv0956377843m_154567971810913444gmail-s1">reduce</span><span class="yiv0956377843m_154567971810913444gmail-s3">([</span><span class="yiv0956377843m_154567971810913444gmail-s6">String</span><span class="yiv0956377843m_154567971810913444gmail-s3"> : </span><span class="yiv0956377843m_154567971810913444gmail-s6">Int</span><span class="yiv0956377843m_154567971810913444gmail-s3">]()) { acc, d </span><span class="yiv0956377843m_154567971810913444gmail-s1">in</span></div>
<div class="yiv0956377843m_154567971810913444gmail-p3"><span class="yiv0956377843m_154567971810913444gmail-s3"> </span><span class="yiv0956377843m_154567971810913444gmail-s1">var</span><span class="yiv0956377843m_154567971810913444gmail-s3"> dx = acc</span></div>
<div class="yiv0956377843m_154567971810913444gmail-p3"><span class="yiv0956377843m_154567971810913444gmail-s3"> </span><span class="yiv0956377843m_154567971810913444gmail-s1">for</span><span class="yiv0956377843m_154567971810913444gmail-s3"> entry </span><span class="yiv0956377843m_154567971810913444gmail-s1">in</span><span class="yiv0956377843m_154567971810913444gmail-s3"> d {</span></div>
<div class="yiv0956377843m_154567971810913444gmail-p3"><span class="yiv0956377843m_154567971810913444gmail-s3"> </span><span class="yiv0956377843m_154567971810913444gmail-s1">let</span><span class="yiv0956377843m_154567971810913444gmail-s3"> key = entry.key</span></div>
<div class="yiv0956377843m_154567971810913444gmail-p3"><span class="yiv0956377843m_154567971810913444gmail-s3"> </span><span class="yiv0956377843m_154567971810913444gmail-s1">guard</span><span class="yiv0956377843m_154567971810913444gmail-s3"> dx[key] == </span><span class="yiv0956377843m_154567971810913444gmail-s1">nil</span><span class="yiv0956377843m_154567971810913444gmail-s3"> </span><span class="yiv0956377843m_154567971810913444gmail-s1">else</span><span class="yiv0956377843m_154567971810913444gmail-s3"> {</span></div>
<div class="yiv0956377843m_154567971810913444gmail-p3"><span class="yiv0956377843m_154567971810913444gmail-s3"> dx[key]! += entry.value</span></div>
<div class="yiv0956377843m_154567971810913444gmail-p4"><span class="yiv0956377843m_154567971810913444gmail-s2"> </span><span class="yiv0956377843m_154567971810913444gmail-s3">continue</span></div>
<div class="yiv0956377843m_154567971810913444gmail-p3"><span class="yiv0956377843m_154567971810913444gmail-s3"> }</span></div>
<div class="yiv0956377843m_154567971810913444gmail-p3"><span class="yiv0956377843m_154567971810913444gmail-s3"> dx[entry.key] = entry.value</span></div>
<div class="yiv0956377843m_154567971810913444gmail-p3"><span class="yiv0956377843m_154567971810913444gmail-s3"> }</span></div>
<div class="yiv0956377843m_154567971810913444gmail-p4"><span class="yiv0956377843m_154567971810913444gmail-s2"> </span><span class="yiv0956377843m_154567971810913444gmail-s3">return</span><span class="yiv0956377843m_154567971810913444gmail-s2"> dx</span></div><div class="yiv0956377843m_154567971810913444gmail-p4">}</div>
<div class="yiv0956377843m_154567971810913444gmail-p2"><span class="yiv0956377843m_154567971810913444gmail-s3"></span><br clear="none"></div>
<div class="yiv0956377843m_154567971810913444gmail-p5"><span class="yiv0956377843m_154567971810913444gmail-s1">print</span><span class="yiv0956377843m_154567971810913444gmail-s2">(</span><span class="yiv0956377843m_154567971810913444gmail-s5">d3</span><span class="yiv0956377843m_154567971810913444gmail-s2">) </span><span class="yiv0956377843m_154567971810913444gmail-s3">// ["Cherries": 9, "Apples": 20, "Oranges": 16]</span></div></div></div><div class="yiv0956377843gmail_extra"><br clear="none"><div class="yiv0956377843gmail_quote">On Fri, Nov 11, 2016 at 9:14 AM, Ray Fix via swift-users <span dir="ltr"><<a rel="nofollow" shape="rect" ymailto="mailto:swift-users@swift.org" target="_blank" href="mailto:swift-users@swift.org">swift-users@swift.org</a>></span> wrote:<br clear="none"><blockquote class="yiv0956377843gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="yiv0956377843yqt1799322064" id="yiv0956377843yqt74643"><div style="word-wrap:break-word;"><div>Hi Mr Bee,</div><div><br clear="none"></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 clear="none"></div><div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(201,27,19);"><span style="color:#c32275;">let</span><span style="color:#000000;"> d1 = [</span><span style="">"Apples"</span><span style="color:#000000;">: </span><span style="color:#0435ff;">20</span><span style="color:#000000;">, </span><span style="">"Oranges"</span><span style="color:#000000;">: </span><span style="color:#0435ff;">13</span><span style="color:#000000;">]</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(201,27,19);"><span style="color:#c32275;">let</span><span style="color:#000000;"> d2 = [</span><span style="">"Oranges"</span><span style="color:#000000;">: </span><span style="color:#0435ff;">3</span><span style="color:#000000;">, </span><span style="">"Cherries"</span><span style="color:#000000;">: </span><span style="color:#0435ff;">9</span><span style="color:#000000;">]</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px;"><span style=""></span><br clear="none"></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(112,61,170);"><span style="color:#c32275;">extension</span><span style="color:#000000;"> </span><span style="">Dictionary</span><span style="color:#000000;"> {</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;"><span style=""> </span><span style="color:#c32275;">func</span><span style=""> merged(with another: [</span><span style="color:#703daa;">Key</span><span style="">: </span><span style="color:#703daa;">Value</span><span style="">]) -> [</span><span style="color:#703daa;">Key</span><span style="">: </span><span style="color:#703daa;">Value</span><span style="">] {</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;"><span style=""> </span><span style="color:#c32275;">var</span><span style=""> result = </span><span style="color:#c32275;">self</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;"><span style=""> </span><span style="color:#c32275;">for</span><span style=""> entry </span><span style="color:#c32275;">in</span><span style=""> another {</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;"><span style=""> result[entry.key] = entry.value</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;"><span style=""> }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;"><span style=""> </span><span style="color:#c32275;">return</span><span style=""> result</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;"><span style=""> }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;"><span style="">}</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px;"><span style=""></span><br clear="none"></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;"><span style="color:#c32275;">let</span><span style=""> result = </span><span style="color:#539aa4;">d1</span><span style="">.</span><span style="color:#294c50;">merged</span><span style="">(with: </span><span style="color:#539aa4;">d2</span><span style="">)</span></div></div><div><br clear="none"></div><br clear="none"><div><blockquote type="cite"><div><div class="yiv0956377843h5"><div>On Nov 11, 2016, at 12:05 AM, Mr Bee via swift-users <<a rel="nofollow" shape="rect" ymailto="mailto:swift-users@swift.org" target="_blank" href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:</div><br clear="none" class="yiv0956377843m_2243869007652622634Apple-interchange-newline"></div></div><div><div><div class="yiv0956377843h5"><div><div style="background-color:rgb(255,255,255);font-family:HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif;font-size:16px;"><div id="yiv0956377843m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9177"><span>Hi,</span></div><div id="yiv0956377843m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9177"><span><br clear="none"></span></div><div dir="ltr" id="yiv0956377843m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9177"><span id="yiv0956377843m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9300">I'm using Swift v3 on an El Capitan machine. I want to merge a dictionary into another compatible dictionary. However, I couldn't find addEntries function in the dictionary instance, like it was on NSMutableDictionary (<a rel="nofollow" shape="rect" target="_blank" href="https://developer.apple.com/reference/foundation/nsmutabledictionary">https://developer.apple.com/ reference/foundation/ nsmutabledictionary</a>). </span></div><div dir="ltr" id="yiv0956377843m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9177"><span><br clear="none"></span></div><div dir="ltr" id="yiv0956377843m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9177"><span id="yiv0956377843m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9391">Does that mean that Swift standard library won'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's native dictionary function (<a rel="nofollow" shape="rect" target="_blank" href="https://developer.apple.com/reference/swift/dictionary">https://developer.apple.com/ reference/swift/dictionary</a>), no need to write a custom function, or bridging to NSMutableDictionary.</span></div><div dir="ltr" id="yiv0956377843m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9177"><span><br clear="none"></span></div><div dir="ltr" id="yiv0956377843m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9177"><span>Thank you.</span></div><div dir="ltr" id="yiv0956377843m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9177"><span><br clear="none"></span></div><div dir="ltr" id="yiv0956377843m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9177"><span>Regards,</span></div><div class="yiv0956377843m_2243869007652622634signature" id="yiv0956377843m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9174"><div id="yiv0956377843m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9175"><br clear="none"></div>–Mr Bee<div id="yiv0956377843m_2243869007652622634yui_3_16_0_ym19_1_1478850922588_9301"><br clear="none"></div></div></div></div></div></div>______________________________ _________________<br clear="none">swift-users mailing list<br clear="none"><a rel="nofollow" shape="rect" ymailto="mailto:swift-users@swift.org" target="_blank" href="mailto:swift-users@swift.org">swift-users@swift.org</a><br clear="none"><a rel="nofollow" shape="rect" target="_blank" href="https://lists.swift.org/mailman/listinfo/swift-users">https://lists.swift.org/ mailman/listinfo/swift-users</a><br clear="none"></div></blockquote></div><br clear="none"></div></div><br clear="none">______________________________ _________________<br clear="none">
swift-users mailing list<br clear="none">
<a rel="nofollow" shape="rect" ymailto="mailto:swift-users@swift.org" target="_blank" href="mailto:swift-users@swift.org">swift-users@swift.org</a><br clear="none">
<a rel="nofollow" shape="rect" target="_blank" href="https://lists.swift.org/mailman/listinfo/swift-users">https://lists.swift.org/ mailman/listinfo/swift-users</a><br clear="none">
<br clear="none"></blockquote></div><br clear="none"></div></div></div><br><br></div> </div> </div> </div></div></body></html>