<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="">That’s a nice one, Erica.</div><div class=""><br class=""></div><div class="">Does this mean there’s nothing ready-made for this in the standard library?</div><div class=""><br class=""></div><div class="">P.S. No need for the builder dictionary. A simple static function (T->U) would be more enough for my use-case.</div><div class=""><br class=""></div><div class="">R+</div><br class=""><div><blockquote type="cite" class=""><div class="">On 28 Dec 2015, at 20:26, Erica Sadun <<a href="mailto:erica@ericasadun.com" class="">erica@ericasadun.com</a>> 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=""><div class="">Maybe something like this?</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">struct</span> KeyedLazyCache<T: Hashable, U> {</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> backingDictionary: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Dictionary</span><<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">U</span>></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> builderDictionary: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Dictionary</span><<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>, () -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">U</span>></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">mutating</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> clear() {<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">backingDictionary</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">removeAll</span>()}</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">mutating</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> valueForKey(key: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>) -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">U</span>? {</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""> <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> value = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">backingDictionary</span>[key] {<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> value}</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""> <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> builder = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">builderDictionary</span>[key] {</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> value = builder()</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">backingDictionary</span>[key] = value</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> value</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><br class=""></div></div>-- E<div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 28, 2015, at 11:36 AM, Rudolf Adamkovič via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> 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 <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><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <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=""> </span>// MARK: Retrieving audio buffers</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></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="">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="" class=""> </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=""> </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=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <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=""> </span>// MARK: Adding audio buffers</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></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="">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="" class=""> </span>addAudioBufferForAssetWithName<span style="" class="">(name)</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="webkit-block-placeholder"></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="">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><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></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="">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=""> <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=r5jpKsi6nat7oa43lpCLi5GRGm2utDkbDscuFklXZ2ceAZlYWV08c3HKIi8NzT2SWnz85g8HbMU9FeLg-2BJLc6Y95ShnenwDnoDwajU-2BKeB2YpG9CmdOOblxbYiSkpwRcjYrLmMz4WJZBqtRVCWsIZTB0PuJl7AufHaUDJarkxPXeeitelx7ZnGVcGXMubpVeM51Ktw28VZP5qhWfooV4SONe58yYzpBRY-2ByAn1dTOOw-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=""><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></div></div></blockquote></div><br class=""></body></html>