<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=""><div class="">All right, here is the outcome:</div><div class=""><br class=""></div><div class=""><a href="https://github.com/salutis/KeyedCache/blob/master/KeyedCache.swift" class="">https://github.com/salutis/KeyedCache/blob/master/KeyedCache.swift</a></div><div class=""><br class=""></div><div class="">Thanks Erica and Donnacha for examples and inspiration!</div><div class=""><br class=""></div><div class="">R+</div><br class=""><div><blockquote type="cite" class=""><div class="">On 28 Dec 2015, at 19:36, 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=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">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 style="font-family: Menlo; font-size: 11px;" class="">[</span><span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">String</span><span style="font-family: Menlo; font-size: 11px;" class="">:&nbsp;</span><span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">AVAudioPCMBuffer</span><span style="font-family: Menlo; font-size: 11px;" class="">]</span>&nbsp;dictionary with a&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">String</span><span style="font-family: Menlo; font-size: 11px;" class="">&nbsp;-&gt;&nbsp;</span><span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">AVAudioPCMBuffer</span>&nbsp;function that generates values as needed:</div><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> PlayerAudioCache {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="" class="">&nbsp; &nbsp; </span>// MARK: Retrieving audio buffers</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></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="">func</span> audioBufferForAssetWithName(name: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span>) -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">AVAudioPCMBuffer</span>? {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span>addAudioBufferForAssetWithNameIfNeeded<span style="" class="">(name)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="" class=""> </span>cachedAudioBuffers<span style="" class="">[name]</span></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; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="" class="">&nbsp; &nbsp; </span>// MARK: Adding audio buffers</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></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="">func</span> addAudioBufferForAssetWithNameIfNeeded(name: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">guard</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">cachedAudioBuffers</span>[name] == <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">else</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span>addAudioBufferForAssetWithName<span style="" class="">(name)</span></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; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></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="">private</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> addAudioBufferForAssetWithName(name: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">guard</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> dataAsset = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">NSDataAsset</span>(name: name) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">else</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">fatalError</span>() }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">cachedAudioBuffers</span>[name] = dataAsset.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">map</span> { URL -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">AVAudioPCMBuffer</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">AVAudioPCMBuffer</span>(contentsOfURL: URL)!</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</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; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></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="">private</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> cachedAudioBuffers: [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span>: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">AVAudioPCMBuffer</span>] = [:]</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</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=j4IybsUIPYHiRcwUyRdFaXrnwdnNfHTbRci39rGZ-2Bb9xP-2FwOAJRvyLZI0IZOaAAW7SbgCeE3-2Fx6f57Tb2tjlb5VJzhAcYVx-2BecTf5-2F1el7chiFL5V4cPEUXfMiDLN6Mcoy0BsJZi5WdK9qMtEL9JTWKhxXTASlPNJm9IaB-2FN8v0MiiFIFgo52i-2F6F1viDXMYj-2B6YEO2cBF2cEgLxMPVVUw-3D-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
</div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>