<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="" style="margin: 0px; line-height: normal;"><div class="">Hi All.</div><div class=""><br class=""></div><div class="">Swift automatically bridges String to char * when calling C functions. For instance, strlen gets translated as:</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);"> public</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">func</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> strlen(</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">_</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> __s: </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">UnsafePointer</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">Int8</span><span class="" style="font-variant-ligatures: no-common-ligatures;">>!) -> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">UInt</span></div><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);"><br class=""></span></div><div class="">I can call it from Swift like this:</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(62, 30, 129);"> strlen</span><span class="" style="font-variant-ligatures: no-common-ligatures;">(</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);">"|"</span><span class="" style="font-variant-ligatures: no-common-ligatures;">)</span></div></div><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;"><br class=""></span></div></div><div class="">I’m But, I’m working with a C struct containing a char *:</div><div class=""><br class=""></div><div class=""><div class="" style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);"> public</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">struct</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> _PQprintOpt {</span></div><div class="" style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; color: rgb(0, 132, 0);"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">public</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">var</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);"> header: </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">pqbool</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);"> </span><span class="" style="font-variant-ligatures: no-common-ligatures;">/* print output field headings and row count */</span></div><div class="" style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; min-height: 13px;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">public</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">var</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> align: </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">pqbool</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="color: rgb(0, 132, 0); font-variant-ligatures: no-common-ligatures;">/* fill align the fields */</span><span class="" style="font-variant-ligatures: no-common-ligatures;"></span></div><div class="" style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; min-height: 13px;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">public</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">var</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> fieldSep: </span><span class="" style="color: rgb(112, 61, 170); font-variant-ligatures: no-common-ligatures;">UnsafeMutablePointer</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><</span><span class="" style="color: rgb(112, 61, 170); font-variant-ligatures: no-common-ligatures;">Int8</span><span class="" style="font-variant-ligatures: no-common-ligatures;">>! </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 132, 0);">/* field separator */</span></div><div class="" style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; color: rgb(0, 132, 0);"> ...</div><div class="" style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal; min-height: 13px;"> }<span class="" style="font-variant-ligatures: no-common-ligatures;"></span></div><div class="" style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);"> public</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">typealias</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> PQprintOpt = </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">_PQprintOpt</span></div><div class=""><br class=""></div><div class="">When I try to assign to fieldSep like this:</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> opt.fieldSep = </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);">"|"</span></div></div><div class=""><br class=""></div><div class="">I get the error:</div><div class=""><br class=""></div><div class=""> Cannot assign value of type 'String' to type 'UnsafeMutablePointer<Int8>!'</div><div class=""> </div></div></div><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;"><div class="">I assume that the difference is that strlen declares const char * and fieldSep is simply char *, so strlen is non-mutable while fieldSep is mutable. Is this correct?</div><div class=""><br class=""></div><div class="">I currently have this ugly hack to get this to work:</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);"> var</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> opt :</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">PQprintOpt</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> = </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">PQprintOpt</span><span class="" style="font-variant-ligatures: no-common-ligatures;">()</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">guard</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">let</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> fieldSeparator = </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);">"|"</span><span class="" style="font-variant-ligatures: no-common-ligatures;">.</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(62, 30, 129);">cString</span><span class="" style="font-variant-ligatures: no-common-ligatures;">(using: .</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">utf8</span><span class="" style="font-variant-ligatures: no-common-ligatures;">) </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">else</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="color: rgb(186, 45, 162); font-variant-ligatures: no-common-ligatures;">throw</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="color: rgb(79, 129, 135); font-variant-ligatures: no-common-ligatures;">Errors</span><span class="" style="font-variant-ligatures: no-common-ligatures;">.</span><span class="" style="color: rgb(49, 89, 93); font-variant-ligatures: no-common-ligatures;">databaseConnectionError</span><span class="" style="font-variant-ligatures: no-common-ligatures;">(</span><span class="" style="color: rgb(209, 47, 27); font-variant-ligatures: no-common-ligatures;">"Could not set field separator"</span><span class="" style="font-variant-ligatures: no-common-ligatures;">)</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> }</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> opt.</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">fieldSep</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> = </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">UnsafeMutablePointer</span><span class="" style="font-variant-ligatures: no-common-ligatures;">(mutating:fieldSeparator)</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><br class=""></div></div><div class=""><div class="">Is there a cleaner way this could work, or should this be considered a compiler bug?</div></div><div class=""><br class=""></div><div class="">Also, why is the conversion to Swift an IUO? NULL is a totally valid value for fieldSep.</div><div class=""><br class=""></div><div class="">Thanks!</div><div class=""><br class=""></div><div class="">-Kenny</div></span></div></body></html>