<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="">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&nbsp;allZeros&nbsp;array.&nbsp;<div class=""><br class=""></div><div class="">Here is the correct way to do it:</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: #ba2da2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> allZerosUUID() -&gt; </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> allZeros = [</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt8</span><span style="font-variant-ligatures: no-common-ligatures" class="">](repeating: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures" class="">, count: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">32</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> allZeros.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">withUnsafeBufferPointer</span><span style="font-variant-ligatures: no-common-ligatures" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">NSUUID</span><span style="font-variant-ligatures: no-common-ligatures" class="">(uuidBytes: $0.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">baseAddress</span><span style="font-variant-ligatures: no-common-ligatures" class="">).</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">uuidString</span><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><br class=""></div><div class=""><div><blockquote type="cite" class=""><div class="">On Mar 1, 2017, at 2:35 PM, Russell Finn via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Thanks to Joe and Quinn for their answers. I have a related followup — a co-worker learning Swift wrote the following function:<br class=""><div class=""><br class="">&nbsp; &nbsp; func allZerosUUID() -&gt; String {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; var allZeros = [UInt8](repeating: 0, count: 32)<br class="">&nbsp; &nbsp; &nbsp; &nbsp; return withUnsafePointer(to: &amp;allZeros) { zerosPtr in<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return NSUUID(uuidBytes: zerosPtr).uuidString<br class="">&nbsp; &nbsp; &nbsp; &nbsp; }<br class="">&nbsp; &nbsp; }<br class=""><br class="">but was puzzled that Xcode 8.2.1 gave an error "Cannot convert value of type 'UnsafePointer&lt;_&gt;' to expected argument type 'UnsafePointer&lt;UInt8&gt;!'" on the line with the NSUUID initializer.&nbsp; Their expectation was that `zerosPtr` would be of type `UnsafePointer&lt;UInt8&gt;` because `allZeros` is of type `[UInt8]`. &nbsp;</div><div class=""><br class=""></div><div class="">They discovered that they could work around this by adding a call to `withMemoryRebound`:<br class=""><br class="">&nbsp; &nbsp; func allZerosUUID() -&gt; String {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; var allZeros = [UInt8](repeating: 0, count: 32)<br class="">&nbsp; &nbsp; &nbsp; &nbsp; return withUnsafePointer(to: &amp;allZeros) { zerosPtr in<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zerosPtr.withMemoryRebound(to: UInt8.self, capacity: allZeros.count) { zerosPtr in<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return NSUUID(uuidBytes: zerosPtr).uuidString<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br class="">&nbsp; &nbsp; &nbsp; &nbsp; }<br class="">&nbsp; &nbsp; }<br class=""><br class="">but felt that this should be unnecessary. Perhaps I'm missing something simple, but I was unable to explain this compiler behavior; can anyone on the list do so?</div><div class=""><br class=""></div><div class="">(Yes, I did point out that they could pass `&amp;allZeros` directly to `NSUUID(uuidBytes:)`.)<br class=""></div><div class="gmail_extra"><div class="gmail_quote"><br class=""></div><div class="gmail_quote">Thanks — Russell</div><div class="gmail_quote"><br class=""></div></div></div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></div></body></html>