<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Sep 25, 2017, at 6:12 PM, Kenny Leung via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">I’m trying to implement the AudioQueue example from Apple in Swift, and keep it as Swifty as I can. I’ve run into a problem where I have a let ivar for the AudioQueue, but the only way to initialize that let ivar is to pass a block to the function that creates the AudioQueue. I get the error, "Variable 'self.mQueue' used before being initialized”. Is there any way to get around this catch?&nbsp;</div></div></div></blockquote><div><br class=""></div><div>The compiler cannot guarantee that the code running inside your block does not try to use mQueue before it is initialized. Swift strictly enforces its initialization rules, so the compiler rejects your code.</div><div><br class=""></div><div>One common solution is to change mQueue into an implicitly-unwrapped optional var. The compiler still can't tell if your block object uses mQueue before it is initialized, but if it does it will get a well-defined runtime error due to the nil IUO. That well-defined error is sufficient to pacify the initialization rules.</div><div><br class=""></div><div>Another solution might be to divide your data into two different types. It looks like the block object only needs to access self.audioBuffers and self.source. You might be able to box those two fields into a separate class type and have the block object capture a reference to that object only. Then the initialization order is simple enough for the compiler to understand:</div><div>1. PDAudioPlayer.init() initializes a source+audioBuffers object.</div><div>2. PDAudioPlayer.init() calls AudioQueueNewOutputWithDispatchQueue(), passing it a block that captures a reference to the fully-initialized source+audioBuffers object.</div><div>3. PDAudioPlayer.init() uses the results of #1 and #2 to complete its own initialization or return nil.</div><div>This adds an extra allocation and extra indirection to each PDAudioPlayer object, but as long as you don't have millions of these things the performance difference should be negligible.</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">———8&lt;————8&lt;————</div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">class</span> PDAudioPlayer {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">static</span> <span style="color: #ba2da2" class="">let</span> kNumberBuffers :<span style="color: #703daa" class="">Int</span> = <span style="color: #272ad8" class="">3</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008400" class="">// 1</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class=""><span style="" class="">&nbsp; &nbsp; </span>//var mDataFormat:AudioStreamBasicDescription &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 2</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">let</span> mQueue :<span style="color: #703daa" class="">AudioQueueRef</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008400" class="">// 3</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class=""><span style="" class="">&nbsp; &nbsp; </span>//var mBuffers :[AudioQueueBufferRef]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 4</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class=""><span style="" class="">&nbsp; &nbsp; </span>//var mAudioFile :AudioFileID?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 5</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">var</span> bufferByteSize :<span style="color: #703daa" class="">UInt32</span>? &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008400" class="">// 6</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">var</span> mCurrentPacket :<span style="color: #703daa" class="">Int64</span>?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008400" class="">// 7</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">var</span> mNumPacketsToRead :<span style="color: #703daa" class="">UInt32</span>?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008400" class="">// 8</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="color: #ba2da2" class="">var</span><span style="" class=""> mPacketDescs :</span>UnsafePointer<span style="" class="">&lt;</span>AudioStreamPacketDescription<span style="" class="">&gt;?&nbsp; </span><span style="color: #008400" class="">// 9</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">var</span> mIsRunning: <span style="color: #703daa" class="">Bool</span> = <span style="color: #ba2da2" class="">false</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008400" class="">// 10</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); 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; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">let</span> dispatchQueue :<span style="color: #703daa" class="">DispatchQueue</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="color: #ba2da2" class="">let</span><span style="" class=""> source :</span>PDFileAudioSource</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">var</span> audioBuffers :[<span style="color: #4f8187" class="">PDAudioBuffer</span>]</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); 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(79, 129, 135); background-color: rgb(255, 255, 255);" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="color: #ba2da2" class="">init</span><span style="" class="">?(source:</span>PDFileAudioSource<span style="" class="">) {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">self</span>.<span style="color: #4f8187" class="">source</span> = source</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">self</span>.<span style="color: #4f8187" class="">dispatchQueue</span> = <span style="color: #703daa" class="">DispatchQueue</span>(label:<span style="color: #d12f1b" class="">"AudioPlayer"</span>, qos:.<span style="color: #ba2da2" class="">default</span>, attributes:[], autoreleaseFrequency:.<span style="color: #3e1e81" class="">workItem</span>, target:<span style="color: #ba2da2" class="">nil</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135); background-color: rgb(255, 255, 255);" class=""><span style="" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ba2da2" class="">self</span><span style="" class="">.</span>audioBuffers<span style="" class=""> = [</span>PDAudioBuffer<span style="" class="">]()</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">var</span> tempAudioQueue :<span style="color: #703daa" class="">AudioQueueRef</span>?</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">var</span> dataFormat = source.<span style="color: #4f8187" class="">mDataFormat</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">let</span> status = <span style="color: #3e1e81" class="">AudioQueueNewOutputWithDispatchQueue</span>(&amp;tempAudioQueue, &amp;dataFormat, <span style="color: #272ad8" class="">0</span>, <span style="color: #ba2da2" class="">self</span>.<span style="color: #4f8187" class="">dispatchQueue</span>) { [weak <span style="text-decoration: underline ; color: #ba2da2" class="">s</span><span style="color: #ba2da2" class="">elf</span>] (queue, buffer) <span style="color: #ba2da2" class="">in &nbsp; // ERROR on this line</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">guard</span> <span style="color: #ba2da2" class="">let</span> this = <span style="color: #ba2da2" class="">self</span> <span style="color: #ba2da2" class="">else</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">return</span> <span style="color: #008400" class="">// block</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">guard</span> <span style="color: #ba2da2" class="">let</span> audioBuffer = this.<span style="color: #31595d" class="">audioBufferForAudioQueueBuffer</span>(aqBuffer:buffer) <span style="color: #ba2da2" class="">else</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">return</span> <span style="color: #008400" class="">// block</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<span style="color: #4f8187" class="">source</span>.<span style="color: #31595d" class="">fillBuffer</span>(audioBuffer)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">if</span> status != <span style="color: #272ad8" class="">0</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">return</span> <span style="color: #ba2da2" class="">nil</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">guard</span> <span style="color: #ba2da2" class="">let</span> audioQueue = tempAudioQueue <span style="color: #ba2da2" class="">else</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">return</span> <span style="color: #ba2da2" class="">nil</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">self</span>.<span style="color: #4f8187" class="">mQueue</span> = audioQueue</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 12px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">private</span> <span style="color: #ba2da2" class="">func</span> audioBufferForAudioQueueBuffer(aqBuffer:<span style="color: #703daa" class="">AudioQueueBufferRef</span>) -&gt; <span style="color: #4f8187" class="">PDAudioBuffer</span>? {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">for</span> buffer <span style="color: #ba2da2" class="">in</span> <span style="color: #ba2da2" class="">self</span>.<span style="color: #4f8187" class="">audioBuffers</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">if</span> buffer.<span style="color: #4f8187" class="">audioQueueBuffer</span> == aqBuffer {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">return</span> buffer</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">return</span> <span style="color: #ba2da2" class="">nil</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div></div></div></div></blockquote></div><br class=""></body></html>