<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">This version kind of mirrors Python’s defaultdict:<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">final</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">class</span> DefaultDict&lt;Key: Hashable, Value&gt;: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">CollectionType</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">CustomStringConvertible</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">private</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> store: [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Key</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Value</span>]</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">private</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> factory: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Key</span> -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Value</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">subscript</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(i: </span>DictionaryIndex<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&lt;</span>Key<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">,</span>Value<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&gt;) -&gt; (</span>Key<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">,</span>Value<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">store</span>[i]</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> startIndex: </span>DictionaryIndex<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&lt;</span>Key<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">,</span>Value<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&gt; { </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">store</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>startIndex<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> endIndex: </span>DictionaryIndex<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&lt;</span>Key<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">,</span>Value<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&gt; { </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">store</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>endIndex<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> generate() -&gt; </span>DictionaryGenerator<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&lt;</span>Key<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">,</span>Value<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&gt; {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">store</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">generate</span>()</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">subscript</span>(key: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Key</span>) -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Value</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">get</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> x = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">store</span>[key] { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> x }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> x = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">factory</span>(key)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">store</span>[key] = x</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> x</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; } <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">set</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">store</span>[key] = newValue</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> def: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Value</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">store</span> = [:]</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">factory</span> = { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> def }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> fac: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Key</span> -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Value</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">store</span> = [:]</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">factory</span> = fac</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> fac: () -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Value</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">store</span> = [:]</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">factory</span> = { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> fac() }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> description: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">store</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">description</span> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> dict = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">DefaultDict</span>&lt;<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Character</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span>&gt;(<span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class="">0</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span> c <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> <span style="font-variant-ligatures: no-common-ligatures; color: #c93108" class="">"abcdgeaaa"</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">characters</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">dict</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span>c<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">]</span> += <span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class="">1</span> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 143, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">dict</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// ["b": 1, "e": 1, "a": 4, "g": 1, "d": 1, "c": 1]</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 143, 0);" class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On 28 Dec 2015, at 19:26, Erica Sadun via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">Maybe something like this?</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(187, 44, 162);">struct</span><span class="Apple-converted-space">&nbsp;</span>KeyedLazyCache&lt;T: Hashable, U&gt; {</div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">var</span><span class="Apple-converted-space">&nbsp;</span>backingDictionary:<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(112, 61, 170);">Dictionary</span>&lt;<span class="" style="color: rgb(112, 61, 170);">T</span>,<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(112, 61, 170);">U</span>&gt;</div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">var</span><span class="Apple-converted-space">&nbsp;</span>builderDictionary:<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(112, 61, 170);">Dictionary</span>&lt;<span class="" style="color: rgb(112, 61, 170);">T</span>, () -&gt;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(112, 61, 170);">U</span>&gt;</div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">mutating</span><span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">func</span><span class="Apple-converted-space">&nbsp;</span>clear() {<span class="" style="color: rgb(79, 129, 135);">backingDictionary</span>.<span class="" style="color: rgb(61, 29, 129);">removeAll</span>()}</div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">mutating</span><span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">func</span><span class="Apple-converted-space">&nbsp;</span>valueForKey(key:<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(112, 61, 170);">T</span>) -&gt;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(112, 61, 170);">U</span>? {</div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">if</span><span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">let</span><span class="Apple-converted-space">&nbsp;</span>value =<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(79, 129, 135);">backingDictionary</span>[key] {<span class="" style="color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space">&nbsp;</span>value}</div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">if</span><span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">let</span><span class="Apple-converted-space">&nbsp;</span>builder =<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(79, 129, 135);">builderDictionary</span>[key] {</div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">let</span><span class="Apple-converted-space">&nbsp;</span>value = builder()</div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(79, 129, 135);">backingDictionary</span>[key] = value</div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space">&nbsp;</span>value</div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">nil</span></div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; }</div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;">}</div><div class="" style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;"><br class=""></div></div><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">-- E</span><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 28, 2015, at 11:36 AM, Rudolf Adamkovič via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">Hi there!<div class=""><br class=""></div><div class="">In my app, I have a very simple class that serves as a key-value cache. The whole thing is basically a lazily populated&nbsp;<span class="" style="font-family: Menlo; font-size: 11px;">[</span><span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">String</span><span class="" style="font-family: Menlo; font-size: 11px;">:&nbsp;</span><span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">AVAudioPCMBuffer</span><span class="" style="font-family: Menlo; font-size: 11px;">]</span>&nbsp;dictionary with a&nbsp;<span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">String</span><span class="" style="font-family: Menlo; font-size: 11px;">&nbsp;-&gt;&nbsp;</span><span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">AVAudioPCMBuffer</span>&nbsp;function that generates values as needed:</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(187, 44, 162);">final</span><span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">class</span><span class="Apple-converted-space">&nbsp;</span>PlayerAudioCache {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span></span>// MARK: Retrieving audio buffers</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">func</span><span class="Apple-converted-space">&nbsp;</span>audioBufferForAssetWithName(name:<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(112, 61, 170);">String</span>) -&gt;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(112, 61, 170);">AVAudioPCMBuffer</span>? {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);"><span class="">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span></span>addAudioBufferForAssetWithNameIfNeeded<span class="">(name)</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);"><span class="">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="color: rgb(187, 44, 162);">return</span><span class=""><span class="Apple-converted-space">&nbsp;</span></span>cachedAudioBuffers<span class="">[name]</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span></span>// MARK: Adding audio buffers</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">func</span><span class="Apple-converted-space">&nbsp;</span>addAudioBufferForAssetWithNameIfNeeded(name:<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(112, 61, 170);">String</span>) {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">guard</span><span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(79, 129, 135);">cachedAudioBuffers</span>[name] ==<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">nil</span><span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">else</span><span class="Apple-converted-space">&nbsp;</span>{<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">return</span><span class="Apple-converted-space">&nbsp;</span>}</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);"><span class="">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span></span>addAudioBufferForAssetWithName<span class="">(name)</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">private</span><span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">func</span><span class="Apple-converted-space">&nbsp;</span>addAudioBufferForAssetWithName(name:<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(112, 61, 170);">String</span>) {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">guard</span><span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">let</span><span class="Apple-converted-space">&nbsp;</span>dataAsset =<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(112, 61, 170);">NSDataAsset</span>(name: name)<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">else</span><span class="Apple-converted-space">&nbsp;</span>{<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(61, 29, 129);">fatalError</span>() }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(79, 129, 135);">cachedAudioBuffers</span>[name] = dataAsset.<span class="" style="color: rgb(49, 89, 93);">map</span><span class="Apple-converted-space">&nbsp;</span>{ URL -&gt;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(112, 61, 170);">AVAudioPCMBuffer</span><span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">in</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(112, 61, 170);">AVAudioPCMBuffer</span>(contentsOfURL: URL)!</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">private</span><span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">var</span><span class="Apple-converted-space">&nbsp;</span>cachedAudioBuffers: [<span class="" style="color: rgb(112, 61, 170);">String</span>:<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(112, 61, 170);">AVAudioPCMBuffer</span>] = [:]</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</div></div><div class=""><br class=""></div><div class="">I feel like there is a pre-made type in Swift’s standard library for what I’m doing here. Am I right?</div><div class=""><br class=""></div><div class="">Ideas? Pointers?</div><div class=""><br class=""></div><div class="">Thank you!</div><div class=""><br class=""></div><div class="">R+</div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=r5jpKsi6nat7oa43lpCLi5GRGm2utDkbDscuFklXZ2ceAZlYWV08c3HKIi8NzT2SWnz85g8HbMU9FeLg-2BJLc6Y95ShnenwDnoDwajU-2BKeB2YpG9CmdOOblxbYiSkpwRcjYrLmMz4WJZBqtRVCWsIZTB0PuJl7AufHaUDJarkxPXeeitelx7ZnGVcGXMubpVeM51Ktw28VZP5qhWfooV4SONe58yYzpBRY-2ByAn1dTOOw-3D" alt="" width="1" height="1" border="0" class="" style="height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;"></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></blockquote></div><br class=""></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=Lo8TP3b1oIn3yQXUt9zA1UCQfR-2BMBCuqnubTuDg47-2B0rhYammNxYzHeraPBEPQClw-2FcpNQ7WeQ4tvR-2B3RAObDHSR6yX1i5xBGKgsHLAcLIL4Nfq63QAKw37bNa-2FAYSsEqmGWvq-2BY0xHTLa-2Fzes6Oglp-2FsrdnkZrXWhyvWpZRN9rCGUnncs4DBdkITcEfpJr9I88-2FY6uq7E4lizdHqHlqds6-2BCjmJ9RcPVVCCAG3T-2F4Y-3D" alt="" width="1" height="1" border="0" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class=""><span class="Apple-converted-space">&nbsp;</span>_______________________________________________</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">swift-users mailing list</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="mailto:swift-users@swift.org" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">swift-users@swift.org</a><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-users</a></div></blockquote></div><br class=""></div></body></html>