<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. &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 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() -> </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=""> </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=""> </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 <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> 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=""> func allZerosUUID() -> String {<br class=""> var allZeros = [UInt8](repeating: 0, count: 32)<br class=""> return withUnsafePointer(to: &allZeros) { zerosPtr in<br class=""> return NSUUID(uuidBytes: zerosPtr).uuidString<br class=""> }<br class=""> }<br class=""><br class="">but was puzzled that Xcode 8.2.1 gave an error "Cannot convert value of type 'UnsafePointer<_>' to expected argument type 'UnsafePointer<UInt8>!'" on the line with the NSUUID initializer. Their expectation was that `zerosPtr` would be of type `UnsafePointer<UInt8>` because `allZeros` is of type `[UInt8]`. </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=""> func allZerosUUID() -> String {<br class=""> var allZeros = [UInt8](repeating: 0, count: 32)<br class=""> return withUnsafePointer(to: &allZeros) { zerosPtr in<br class=""> zerosPtr.withMemoryRebound(to: UInt8.self, capacity: allZeros.count) { zerosPtr in<br class=""> return NSUUID(uuidBytes: zerosPtr).uuidString<br class=""> }<br class=""> }<br class=""> }<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 `&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>