<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="">Hi Howard.</div><div class=""><br class=""></div><div class="">Yes, this would make it compile, but it still would not solve the issues that make it wrong in my mind:</div><div class=""><br class=""></div><div class="">1. I would have to make it a var, but it should not be mutable.</div><div class=""><br class=""></div><div class="">2. It’s not optional. Without it, the PDAudioPlayer class could not function.</div><div class=""><br class=""></div><div class="">Of course, this is just being stubbornly pedantic about the code, &nbsp;but the whole point of this project is an exercise in pedantry.</div><div class=""><br class=""></div><div class="">-Kenny</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Sep 25, 2017, at 10:35 PM, Howard Lovatt &lt;<a href="mailto:howard.lovatt@gmail.com" class="">howard.lovatt@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><div dir="auto" class="">You could try making mQueue a force unwrapped optional or initialize it to a failed queue before overwriting on last line of `init`.&nbsp;</div><br class=""><div class="gmail_quote"><div class="">On Tue, 26 Sep 2017 at 11:39 am, Kenny Leung via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class="">Hi Muthu.</div><div class=""><br class=""></div><div class="">Thanks for the suggestion.</div><div class=""><br class=""></div><div class="">I don’t want to do that because failure to create the AudioQueue should mean failure to create the AudioPlayer itself.</div></div><div style="word-wrap:break-word" class=""><div class=""><br class=""></div><div class="">-Kenny</div></div><div style="word-wrap:break-word" class=""><div class=""><br class=""></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Sep 25, 2017, at 6:33 PM, somu subscribe &lt;<a href="mailto:somu.subscribe@gmail.com" target="_blank" class="">somu.subscribe@gmail.com</a>&gt; wrote:</div><br class="m_-4948567996568823203Apple-interchange-newline"><div class=""><div style="word-wrap:break-word" class=""><div class="">Hi Kenny,</div><div class=""><br class=""></div><div class="">You could use a lazy var and initialise it with a closure.</div><div class=""><br class=""></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;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;color:rgb(0,132,0);background-color:rgb(255,255,255)" class=""><span class="">&nbsp; &nbsp; </span>//Would be created lazily when first invoked</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="">lazy</span> <span style="color:#ba2da2" class="">var</span> mQueue : <span style="color:#703daa" class="">AudioQueueRef</span> = {</div><div style="margin:0px;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;color:rgb(0,132,0);background-color:rgb(255,255,255)" class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span>//create an audioQueue and return that instance</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> 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:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class="">}</div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><span style="font-family:Helvetica;font-size:12px" class="">Thanks and regards,</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><span style="font-family:Helvetica;font-size:12px" class="">Muthu</span></div><div class=""><br class=""></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 26 Sep 2017, at 9:12 AM, Kenny Leung via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="m_-4948567996568823203Apple-interchange-newline"><div class=""><div style="word-wrap:break-word" class="">Hi All.<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 class=""><br class=""></div><div class="">Thanks!</div><div class=""><br class=""></div><div class="">-Kenny</div><div class=""><br class=""></div><div class="">The code follows.</div><div class=""><br class=""></div><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 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 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 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 class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">var</span><span class=""> mPacketDescs :</span>UnsafePointer<span class="">&lt;</span>AudioStreamPacketDescription<span 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="m_-4948567996568823203webkit-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 class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">let</span><span 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="m_-4948567996568823203webkit-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 class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">init</span><span class="">?(source:</span>PDFileAudioSource<span 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 class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#ba2da2" class="">self</span><span class="">.</span>audioBuffers<span class=""> = [</span>PDAudioBuffer<span 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 class=""><br class=""></div></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div>_______________________________________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class="">
</blockquote></div></div><div dir="ltr" class="">-- <br class=""></div><div class="gmail_signature" data-smartmail="gmail_signature">-- Howard.</div>
</div></blockquote></div><br class=""></body></html>