<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 dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div><blockquote type="cite" class=""><div class="">On Mar 1, 2017, at 14:23, Kenny Leung 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=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div 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;"> </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;"> 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;"> </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></span></div></div></div></blockquote><div><br class=""></div><div class="">Hey, Kenny. The const vs non-const part is important, since both the implicit conversion and cString(using:) are allowed to return a pointer to the internal data being used by the String, and modifying that would be breaking the rules (and could potentially cause a crash). In the case of 'fieldSep', it's unlikely that anyone is going to mutate the contents of the string; it was probably just the original author (you?) not bothering to be const-correct. If you control this struct, a better fix would be to use 'const char *' for the field.</div><div><br class=""></div><div>That said, that's not the main isuse. The implicit conversion from String to UnsafePointer<CChar> is only valid when the string is used as a function argument, because the conversion might need to allocate temporary storage. In that case, it’s important to know when it’s safe to <i class="">deallocate</i> that storage. For a function call, that’s when the call returns, but for storing into a struct field it’s completely unbounded. So there’s no implicit conversion there.</div><div><br class=""></div><div>If you’re willing to limit your use of the pointer value to a single block of code, you can use <a href="https://developer.apple.com/reference/swift/string/1538904-withcstring" class="">withCString</a>:</div><div><br class=""></div></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div><div>myString.withCString {</div><div> var opt :PQprintOpt = PQprintOpt()</div><div> opt.fieldSep = UnsafeMutablePointer(mutating: $0)</div><div> // use 'opt'</div><div>}</div></div></div></blockquote><div class=""><br class=""></div>Note that it is illegal to persist the pointer value beyond the execution of withCString, because it might point to a temporary buffer.<br class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div><div><br class=""></div><div>Alternately, if you’re willing to call free() later, you can use the C function strdup:</div><div><br class=""></div></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div><div>opt.fieldSep = strdup(myString)</div></div></div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class="">// use ‘opt’</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class="">free(opt.fieldSep)</blockquote><br class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div><div><br class=""></div><div>Of course, all of this is overkill for a string <i class="">literal.</i> Perhaps when Swift gets conditional conformances, we could consider making UnsafePointer<CChar> conform to ExpressibleByStringLiteral. Meanwhile, you can use the type designed specifically for this purpose, <a href="https://developer.apple.com/reference/swift/staticstring" class="">StaticString</a>:</div><div><br class=""></div></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div><div>let separator: StaticString = “|”</div><div>opt.fieldSep = UnsafeMutablePointer(mutating: separator.utf8start)</div></div></div></blockquote><div class=""><br class=""></div>Note that you <i class="">still</i> have to do the init(mutating:) workaround for the fact that you’re not allowed to mutate this string.<br class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;"><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></span></div></div></div></blockquote><br class=""></div><div>You're welcome to mark that field as _Nullable on the C side, but without annotations Swift makes very few assumptions about pointers imported from C.</div><div><br class=""></div><div>Hope that helps!</div><div>Jordan</div><br class=""></div></body></html>