<div><div dir="auto">I know were you are coming from, but:</div><div dir="auto"><br></div><div dir="auto"> 1. In other languages it would be the equivalent of an force unwrapped optional and you could get a NullPointerException (or worse - core dump). Using a force unwrapped optional just levels the playing field!</div><div dir="auto"><br></div><div dir="auto"> 2. You could make it private(set) which would de-risk the var bit. </div><div dir="auto"><br></div><div dir="auto"> 3. See below. </div><div dir="auto"><br></div><div dir="auto"><div dir="auto">class AssignOnce<T: AnyObject> {</div><div dir="auto"> weak var value: T? = nil { // Use a weak var to avoid a retain cycle.</div><div dir="auto"> willSet {</div><div dir="auto"> guard value == nil else {</div><div dir="auto"> fatalError("Can only be set once.")</div><div dir="auto"> }</div><div dir="auto"> }</div><div dir="auto"> }</div><div dir="auto">}</div><div dir="auto">class InitSelfDependency {</div><div dir="auto"> let dependsUponSelf = AssignOnce<InitSelfDependency>()</div><div dir="auto"> init() {</div><div dir="auto"> dependsUponSelf.value = self</div><div dir="auto"> }</div><div dir="auto">}</div><div dir="auto">let i = InitSelfDependency()</div><div dir="auto">i.dependsUponSelf</div><div dir="auto"><br></div></div><br><div class="gmail_quote"><div>On Tue, 26 Sep 2017 at 4:44 pm, Kenny Leung via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br></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"><div>Hi Howard.</div><div><br></div><div>Yes, this would make it compile, but it still would not solve the issues that make it wrong in my mind:</div><div><br></div><div>1. I would have to make it a var, but it should not be mutable.</div><div><br></div><div>2. It’s not optional. Without it, the PDAudioPlayer class could not function.</div><div><br></div><div>Of course, this is just being stubbornly pedantic about the code, but the whole point of this project is an exercise in pedantry.</div></div><div style="word-wrap:break-word"><div><br></div><div>-Kenny</div></div><div style="word-wrap:break-word"><div><br></div><br><div><blockquote type="cite"><div>On Sep 25, 2017, at 10:35 PM, Howard Lovatt <<a href="mailto:howard.lovatt@gmail.com" target="_blank">howard.lovatt@gmail.com</a>> wrote:</div><br class="m_3590976574028968439Apple-interchange-newline"><div><div><div dir="auto">You could try making mQueue a force unwrapped optional or initialize it to a failed queue before overwriting on last line of `init`. </div><br><div class="gmail_quote"><div>On Tue, 26 Sep 2017 at 11:39 am, Kenny Leung via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:<br></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"><div>Hi Muthu.</div><div><br></div><div>Thanks for the suggestion.</div><div><br></div><div>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"><div><br></div><div>-Kenny</div></div><div style="word-wrap:break-word"><div><br></div><br><div><blockquote type="cite"><div>On Sep 25, 2017, at 6:33 PM, somu subscribe <<a href="mailto:somu.subscribe@gmail.com" target="_blank">somu.subscribe@gmail.com</a>> wrote:</div><br class="m_3590976574028968439m_-4948567996568823203Apple-interchange-newline"><div><div style="word-wrap:break-word"><div>Hi Kenny,</div><div><br></div><div>You could use a lazy var and initialise it with a closure.</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:#ba2da2">class</span> PDAudioPlayer {</div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></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)"><span> </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)"> <span style="color:#ba2da2">lazy</span> <span style="color:#ba2da2">var</span> mQueue : <span style="color:#703daa">AudioQueueRef</span> = {</div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></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)"><span> </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)"> <span style="color:#ba2da2">return</span> audioQueue</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }()</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">}</div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-family:Helvetica;font-size:12px">Thanks and regards,</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-family:Helvetica;font-size:12px">Muthu</span></div><div><br></div><br><div><blockquote type="cite"><div>On 26 Sep 2017, at 9:12 AM, Kenny Leung via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:</div><br class="m_3590976574028968439m_-4948567996568823203Apple-interchange-newline"><div><div style="word-wrap:break-word">Hi All.<div><br></div><div>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? </div><div><br></div><div>Thanks!</div><div><br></div><div>-Kenny</div><div><br></div><div>The code follows.</div><div><br></div><div>———8<————8<————</div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:#ba2da2">class</span> PDAudioPlayer {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">static</span> <span style="color:#ba2da2">let</span> kNumberBuffers :<span style="color:#703daa">Int</span> = <span style="color:#272ad8">3</span> <span style="color:#008400">// 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)"><span> </span>//var mDataFormat:AudioStreamBasicDescription // 2</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">let</span> mQueue :<span style="color:#703daa">AudioQueueRef</span> <span style="color:#008400">// 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)"><span> </span>//var mBuffers :[AudioQueueBufferRef] // 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)"><span> </span>//var mAudioFile :AudioFileID? // 5</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">var</span> bufferByteSize :<span style="color:#703daa">UInt32</span>? <span style="color:#008400">// 6</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">var</span> mCurrentPacket :<span style="color:#703daa">Int64</span>? <span style="color:#008400">// 7</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">var</span> mNumPacketsToRead :<span style="color:#703daa">UInt32</span>? <span style="color:#008400">// 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)"><span> </span><span style="color:#ba2da2">var</span><span> mPacketDescs :</span>UnsafePointer<span><</span>AudioStreamPacketDescription<span>>? </span><span style="color:#008400">// 9</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">var</span> mIsRunning: <span style="color:#703daa">Bool</span> = <span style="color:#ba2da2">false</span> <span style="color:#008400">// 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"> <br class="m_3590976574028968439m_-4948567996568823203webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">let</span> dispatchQueue :<span style="color:#703daa">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)"><span> </span><span style="color:#ba2da2">let</span><span> source :</span>PDFileAudioSource</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">var</span> audioBuffers :[<span style="color:#4f8187">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"> <br class="m_3590976574028968439m_-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)"><span> </span><span style="color:#ba2da2">init</span><span>?(source:</span>PDFileAudioSource<span>) {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">self</span>.<span style="color:#4f8187">source</span> = source</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">self</span>.<span style="color:#4f8187">dispatchQueue</span> = <span style="color:#703daa">DispatchQueue</span>(label:<span style="color:#d12f1b">"AudioPlayer"</span>, qos:.<span style="color:#ba2da2">default</span>, attributes:[], autoreleaseFrequency:.<span style="color:#3e1e81">workItem</span>, target:<span style="color:#ba2da2">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)"><span> </span><span style="color:#ba2da2">self</span><span>.</span>audioBuffers<span> = [</span>PDAudioBuffer<span>]()</span></div><div style="margin:0px;font-size:12px;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">var</span> tempAudioQueue :<span style="color:#703daa">AudioQueueRef</span>?</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">var</span> dataFormat = source.<span style="color:#4f8187">mDataFormat</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">let</span> status = <span style="color:#3e1e81">AudioQueueNewOutputWithDispatchQueue</span>(&tempAudioQueue, &dataFormat, <span style="color:#272ad8">0</span>, <span style="color:#ba2da2">self</span>.<span style="color:#4f8187">dispatchQueue</span>) { [weak <span style="text-decoration:underline;color:#ba2da2">s</span><span style="color:#ba2da2">elf</span>] (queue, buffer) <span style="color:#ba2da2">in // 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)"> <span style="color:#ba2da2">guard</span> <span style="color:#ba2da2">let</span> this = <span style="color:#ba2da2">self</span> <span style="color:#ba2da2">else</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">return</span> <span style="color:#008400">// block</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">guard</span> <span style="color:#ba2da2">let</span> audioBuffer = this.<span style="color:#31595d">audioBufferForAudioQueueBuffer</span>(aqBuffer:buffer) <span style="color:#ba2da2">else</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">return</span> <span style="color:#008400">// block</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> this.<span style="color:#4f8187">source</span>.<span style="color:#31595d">fillBuffer</span>(audioBuffer)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">if</span> status != <span style="color:#272ad8">0</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">return</span> <span style="color:#ba2da2">nil</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">guard</span> <span style="color:#ba2da2">let</span> audioQueue = tempAudioQueue <span style="color:#ba2da2">else</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">return</span> <span style="color:#ba2da2">nil</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">self</span>.<span style="color:#4f8187">mQueue</span> = audioQueue</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:12px;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">private</span> <span style="color:#ba2da2">func</span> audioBufferForAudioQueueBuffer(aqBuffer:<span style="color:#703daa">AudioQueueBufferRef</span>) -> <span style="color:#4f8187">PDAudioBuffer</span>? {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">for</span> buffer <span style="color:#ba2da2">in</span> <span style="color:#ba2da2">self</span>.<span style="color:#4f8187">audioBuffers</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">if</span> buffer.<span style="color:#4f8187">audioQueueBuffer</span> == aqBuffer {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">return</span> buffer</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">return</span> <span style="color:#ba2da2">nil</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">}</div></div><div><br></div></div>_______________________________________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div></div></blockquote></div><br></div>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
</blockquote></div></div><div>-- <br></div><div class="m_3590976574028968439gmail_signature" data-smartmail="gmail_signature">-- Howard.</div>
</div></blockquote></div><br></div>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
</blockquote></div></div><div dir="ltr">-- <br></div><div class="gmail_signature" data-smartmail="gmail_signature">-- Howard.</div>