<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="">I’m bridging a C API (that I wrote) to Swift; I’ve got most of it together, but I’m stuck on one part, where I can’t get the bridging code to compile without errors no matter how I write it.</div><div class=""><br class=""></div><div class="">The basic problem is I’ve got a dynamically-allocated opaque C pointer type (C4Key*), and there’s a struct in the C API with a field that points to a C array of these, plus a count. In Swift I have an array of these pointers, typed as [COpaquePointer], and I want to point the struct field to it.</div><div class=""><br class=""></div><div class="">Here’s the relevant bit of the C API:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: #005493" class="">typedef</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #005493" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #343893" class="">c4Key</span><span style="font-variant-ligatures: no-common-ligatures" class=""> C4Key;&nbsp;</span><i style="color: rgb(0, 132, 0);" class="">// opaque type</i></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: #343893" class="">C4Key</span><span style="font-variant-ligatures: no-common-ligatures" class="">* c4key_new(</span><span style="font-variant-ligatures: no-common-ligatures; color: #005493" class="">void</span><span style="font-variant-ligatures: no-common-ligatures" class="">);</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div></span></div><div class=""><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas; color: rgb(0, 84, 147);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures" class="">typedef</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; <font color="#703daa" class="">// ...</font></span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #005493" class="">const</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #343893" class="">C4Key</span><span style="font-variant-ligatures: no-common-ligatures" class=""> **keys;</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">size_t</span><span style="font-variant-ligatures: no-common-ligatures" class=""> keysCount;</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; } C4QueryOptions;</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">In my Swift code I have:</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: #005493" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> opt = </span><span style="font-variant-ligatures: no-common-ligatures; color: #343893" class="">C4QueryOptions</span><span style="font-variant-ligatures: no-common-ligatures" class="">()</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #005493" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> keyHandles: [</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">COpaquePointer</span><span style="font-variant-ligatures: no-common-ligatures" class="">] = </span><span style="font-variant-ligatures: no-common-ligatures; color: #80257f" class="">wrappedKeys</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">map</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {$0.</span><span style="font-variant-ligatures: no-common-ligatures; color: #80257f" class="">handle</span><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas;" class=""><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures" class=""><i class="">// Attempt 1, error is&nbsp;</i></span><i class="">“Cannot assign [COpaquePointer] to COpaquePointer”:</i></div><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; &nbsp; &nbsp; opt.keys = &amp;keyHandles</span></div><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures" class=""><i class="">// Attempt 2, error is "</i></span><span style="font-family: Menlo; font-size: 11px; text-indent: -12px;" class="">cannot assign value of type 'UnsafePointer&lt;[COpaquePointer]&gt;' (aka 'UnsafePointer&lt;Array&lt;COpaquePointer&gt;&gt;') to type 'UnsafeMutablePointer&lt;COpaquePointer&gt;'"</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">withUnsafePointer</span><span style="font-variant-ligatures: no-common-ligatures" class="">(&amp;keyHandles) { keysPtr </span><span style="font-variant-ligatures: no-common-ligatures; color: #005493" class="">in</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opt.keys = keysPtr</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoSomethingWith(opt)</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">It’s definitely a type-checking problem; I can’t convince the compiler to give me a pointer to the elements of the array as a COpaquePointer. Any clues?</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">—Jens</span></div></span></div></span></div></body></html>