<div dir="ltr">Ahh ... which is exactly implied by the documentation I quoted, except that I&#39;m so used to doing it the other way (with `var` and `&amp;`) that I failed to interpret what the text was saying.  Thanks for the tip.</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Mar 2, 2017 at 4:43 PM, Hooman Mehr <span dir="ltr">&lt;<a href="mailto:hooman@mac.com" target="_blank">hooman@mac.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Yes, the easiest way is to rely on compiler magic for ObjC/C interoperability, but it is also important to know what is really happening.<div><br></div><div>My preferred version of the compiler magic is this actually:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">func</span><span style="font-variant-ligatures:no-common-ligatures"> allZerosUUID() -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">String</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">NSUUID</span><span style="font-variant-ligatures:no-common-ligatures">(uuidBytes: [</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">UInt8</span><span style="font-variant-ligatures:no-common-ligatures">](repeating: </span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8">0</span><span style="font-variant-ligatures:no-common-ligatures">, count: </span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8">32</span><span style="font-variant-ligatures:no-common-ligatures">)).</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">uuidString</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><span style="font-variant-ligatures:no-common-ligatures">Which is effectively this:</span></div><div><br></div><div><span class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">func</span><span style="font-variant-ligatures:no-common-ligatures"> allZerosUUID() -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">String</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">let</span><span style="font-variant-ligatures:no-common-ligatures"> allZeros = [</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">UInt8</span><span style="font-variant-ligatures:no-common-ligatures">](repeating: </span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8">0</span><span style="font-variant-ligatures:no-common-ligatures">, count: </span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8">32</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div></span><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">NSUUID</span><span style="font-variant-ligatures:no-common-ligatures">(uuidBytes: allZeros).</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">uuidString</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div>(Note the use of <span style="color:rgb(186,45,162);font-family:Menlo;font-size:11px">let</span> and absence of &amp;).</div><div><div class="h5"><div><br><div><blockquote type="cite"><div>On Mar 2, 2017, at 1:33 PM, Russell Finn &lt;<a href="mailto:rsfinn@gmail.com" target="_blank">rsfinn@gmail.com</a>&gt; wrote:</div><br class="m_7169980660861331325Apple-interchange-newline"><div><div dir="ltr"><div>I agree that the use of `withUnsafeBufferPointer` to get to the contents of an array makes more sense than `withUnsafePointer`, once you discover the concept of a &quot;buffer pointer&quot; (which a new Swift programmer may not do at first).</div><div><br></div>However, I note that the following code:<div><br></div><div>    func allZerosUUID() -&gt; String {</div><div><div>        var allZeros = [UInt8](repeating: 0, count: 32)</div><div>        return NSUUID(uuidBytes: &amp;allZeros)</div><div>    }</div></div><div><br></div><div>also compiles without error and returns the intended value. This appears to be supported by the &quot;Constant Pointers&quot; section of the &quot;Interacting with C APIs&quot; chapter of &quot;Using Swift with Cocoa and Objective-C&quot;:  &quot;When a function is declared as taking an `UnsafePointer&lt;Type&gt;` argument, it can accept ... a `[Type]` value, which is passed as a pointer to the start of the array.&quot;</div><div><br></div><div>I suspect the availability of `&amp;array` in this context, coupled with an awareness of the existence of `withUnsafePointer(to:)`, is what led my co-worker down the wrong road (and to a puzzling error message); but since we have now discovered two more appropriate ways to write the code in question, perhaps it&#39;s not worth worrying about too much.</div><div><br></div><div>Thanks to everyone for the discussion.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Mar 1, 2017 at 7:36 PM, Hooman Mehr <span dir="ltr">&lt;<a href="mailto:hooman@mac.com" target="_blank">hooman@mac.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Your co-worker needs to get passed the learning curve of these “unsafe” APIs and note that Swift arrays are complex data structures. &amp;allZeros does not give you a pointer to a bunch of zero bytes, but a pointer to a struct that contains the private implementation details of allZeros array. <div><br></div><div>Here is the correct way to do it:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">func</span><span style="font-variant-ligatures:no-common-ligatures"> allZerosUUID() -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">String</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">let</span><span style="font-variant-ligatures:no-common-ligatures"> allZeros = [</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">UInt8</span><span style="font-variant-ligatures:no-common-ligatures">](repeating: </span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8">0</span><span style="font-variant-ligatures:no-common-ligatures">, count: </span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8">32</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">return</span><span style="font-variant-ligatures:no-common-ligatures"> allZeros.</span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81">withUnsafeBufferPoint<wbr>er</span><span style="font-variant-ligatures:no-common-ligatures"> { </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">NSUUID</span><span style="font-variant-ligatures:no-common-ligatures">(uuidBytes: $0.</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">baseAddress</span><span style="font-variant-ligatures:no-common-ligatures">).</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">uuidString</span><span style="font-variant-ligatures:no-common-ligatures"> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><br></div><div><div><blockquote type="cite"><div><div class="m_7169980660861331325h5"><div>On Mar 1, 2017, at 2:35 PM, Russell Finn via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br class="m_7169980660861331325m_-8311670139065548302Apple-interchange-newline"></div></div><div><div><div class="m_7169980660861331325h5"><div dir="ltr">Thanks to Joe and Quinn for their answers. I have a related followup — a co-worker learning Swift wrote the following function:<br><div><br>    func allZerosUUID() -&gt; String {<br>        var allZeros = [UInt8](repeating: 0, count: 32)<br>        return withUnsafePointer(to: &amp;allZeros) { zerosPtr in<br>            return NSUUID(uuidBytes: zerosPtr).uuidString<br>        }<br>    }<br><br>but was puzzled that Xcode 8.2.1 gave an error &quot;Cannot convert value of type &#39;UnsafePointer&lt;_&gt;&#39; to expected argument type &#39;UnsafePointer&lt;UInt8&gt;!&#39;&quot; on the line with the NSUUID initializer.  Their expectation was that `zerosPtr` would be of type `UnsafePointer&lt;UInt8&gt;` because `allZeros` is of type `[UInt8]`.  </div><div><br></div><div>They discovered that they could work around this by adding a call to `withMemoryRebound`:<br><br>    func allZerosUUID() -&gt; String {<br>        var allZeros = [UInt8](repeating: 0, count: 32)<br>        return withUnsafePointer(to: &amp;allZeros) { zerosPtr in<br>            zerosPtr.withMemoryRebound(to: UInt8.self, capacity: allZeros.count) { zerosPtr in<br>                return NSUUID(uuidBytes: zerosPtr).uuidString<br>            }<br>        }<br>    }<br><br>but felt that this should be unnecessary. Perhaps I&#39;m missing something simple, but I was unable to explain this compiler behavior; can anyone on the list do so?</div><div><br></div><div>(Yes, I did point out that they could pass `&amp;allZeros` directly to `NSUUID(uuidBytes:)`.)<br></div><div class="gmail_extra"><div class="gmail_quote"><br></div><div class="gmail_quote">Thanks — Russell</div><div class="gmail_quote"><br></div></div></div></div></div><span>
______________________________<wbr>_________________<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/mailma<wbr>n/listinfo/swift-users</a><br></span></div></blockquote></div><br></div></div></blockquote></div><br></div>
</div></blockquote></div><br></div></div></div></div></div></blockquote></div><br></div>