<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="">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 <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="">: </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> dictionary with a <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=""> -> </span><span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">AVAudioPCMBuffer</span> 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><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// MARK: Retrieving audio buffers</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><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="">func</span> audioBufferForAssetWithName(name: <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; color: rgb(49, 89, 93);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>addAudioBufferForAssetWithNameIfNeeded<span style="font-variant-ligatures: no-common-ligatures; color: #000000" 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="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </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>cachedAudioBuffers<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">[name]</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// MARK: Adding audio buffers</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><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="">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=""> <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="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>addAudioBufferForAssetWithName<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(name)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><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="">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=""> <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=""> <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 -> <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=""> <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=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><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="">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><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><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></body></html>