<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">Thank you very much Marco. But What is  “outside of an initializer” really bothers me. **Both** `func bar(keysAndValues:Dictionary&lt;String, String&gt;)` works now. **Are they really outside ?**</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif"><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(186,45,162)">struct<span style="color:rgb(0,0,0)"> Foo {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">    <span style="color:rgb(186,45,162)">var</span> keys = [<span style="color:rgb(209,47,27)">&quot;z&quot;</span>,<span style="color:rgb(209,47,27)">&quot;y&quot;</span>,<span style="color:rgb(209,47,27)">&quot;x&quot;</span>]</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">    {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">        <span style="color:rgb(186,45,162)">didSet</span> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">            <span style="color:rgb(79,129,135)">keys</span>.<span style="color:rgb(62,30,129)">sort</span>()</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">        }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">    }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);min-height:13px">    </p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">    <span style="color:rgb(186,45,162)">init</span>(keysAndValues:<span style="color:rgb(112,61,170)">Dictionary</span>&lt;<span style="color:rgb(112,61,170)">String</span>, <span style="color:rgb(112,61,170)">String</span>&gt;) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">        <span style="color:rgb(186,45,162)">func</span> bar(keysAndValues:<span style="color:rgb(112,61,170)">Dictionary</span>&lt;<span style="color:rgb(112,61,170)">String</span>, <span style="color:rgb(112,61,170)">String</span>&gt;) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">            <span style="color:rgb(79,129,135)">keys</span>.<span style="color:rgb(62,30,129)">append</span>(contentsOf: keysAndValues.<span style="color:rgb(112,61,170)">keys</span>)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">        }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">        <span style="color:rgb(49,89,93)">bar</span>(keysAndValues:keysAndValues)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">    }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);min-height:13px">    </p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)">//    private mutating func bar(keysAndValues:Dictionary&lt;String, String&gt;) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)">//        keys.append(contentsOf: keysAndValues.keys)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)">//    }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">}</p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="color:rgb(186,45,162)">let</span> keysAndValues:<span style="color:rgb(112,61,170)">Dictionary</span>&lt;<span style="color:rgb(112,61,170)">String</span>,<span style="color:rgb(112,61,170)">String</span>&gt; = [<span style="color:rgb(209,47,27)">&quot;c&quot;</span>:<span style="color:rgb(209,47,27)">&quot;c&quot;</span>, <span style="color:rgb(209,47,27)">&quot;b&quot;</span>:<span style="color:rgb(209,47,27)">&quot;b&quot;</span>, <span style="color:rgb(209,47,27)">&quot;a&quot;</span>:<span style="color:rgb(209,47,27)">&quot;a&quot;</span>]</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(186,45,162)">var</span><span style="color:rgb(0,0,0)"> foo = </span><span style="color:rgb(79,129,135)">Foo</span><span style="color:rgb(0,0,0)">(keysAndValues: </span><span style="color:rgb(79,129,135)">keysAndValues</span><span style="color:rgb(0,0,0)">) </span>// `let foo` is the same result</p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Helvetica;min-height:14px"><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(62,30,129)"><span style="color:rgb(79,129,135)">foo</span><span style="color:rgb(0,0,0)">.</span><span style="color:rgb(79,129,135)">keys</span><span style="color:rgb(0,0,0)">.</span>forEach<span style="color:rgb(0,0,0)"> { </span>print<span style="color:rgb(0,0,0)">($0) }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)">/*</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"> prints</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"> a</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"> b</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"> c</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"> x</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"> y</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"> z</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"> */</p></div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Zhao Xin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jul 8, 2017 at 9:59 AM, Marco S Hyman <span dir="ltr">&lt;<a href="mailto:marc@snafu.org" target="_blank">marc@snafu.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
&gt;     init(keysAndValues:Dictionary&lt;<wbr>String, String&gt;) {<br>
&gt;         self.keys.append(contentsOf: keysAndValues.keys)<br>
&gt;     }<br>
<br>
&gt;<br>
</span><span class="">&gt; Above code doesn&#39;t call `didSet` in playground. My .swift file is similar and didn&#39;t call `didSet` either. However, if without a struct, `didSet` is called.<br>
<br>
</span>“If you don’t need to compute the property but still need to provide code that is run before and after setting a new value, use willSet and didSet. The code you provide is run any time the value changes outside of an initializer.”<br>
<br>
Excerpt From: Apple Inc. “The Swift Programming Language (Swift 3.1).” iBooks. <a href="https://itun.es/us/jEUH0.l" rel="noreferrer" target="_blank">https://itun.es/us/jEUH0.l</a><br>
<br>
Note “outside of an initializer”.  didSet and willSet are not called in initializers.<br>
<br>
<br>
<br>
</blockquote></div><br></div>