<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Mar 1, 2017, at 7:08 PM, 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="">Hi Jordan.</div><div class=""><br class=""></div><div class="">Thanks for the lengthy answer.</div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Mar 1, 2017, at 6:21 PM, Jordan Rose <<a href="mailto:jordan_rose@apple.com" class="">jordan_rose@apple.com</a>> wrote:</div><div class=""><div dir="auto" applecontenteditable="true" class="" style="font-family: Helvetica; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class=""><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></div></div></blockquote><div class=""><br class=""></div><div class="">This is the PostgreSQL client library, so I don’t really want to change it. (Although the source is available. Maybe I should submit a patch…)</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="auto" applecontenteditable="true" class="" style="font-family: Helvetica; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class="">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<span class="Apple-converted-space"> </span><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 class=""><br class=""></div><div class="">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 class=""><br class=""></div></div></div><blockquote class="" style="font-family: Helvetica; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div dir="auto" applecontenteditable="true" class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class="">myString.withCString {</div><div class=""> var opt :PQprintOpt = PQprintOpt()</div><div class=""> opt.fieldSep = UnsafeMutablePointer(mutating: $0)</div><div class=""> // use 'opt'</div><div class="">}</div></div></div></blockquote></div></blockquote><div class=""><br class=""></div><div class="">Unfortunately, this is not always an option, since there are multiple char * files in PQprintOpt. Or could I just nest .withCString calls? Looks ugly, but might work, I guess.</div></div></div></div></blockquote></div><br class=""><div class=""><div class="">Here are some basic recommendations for converting between String and C string representations:</div><div class=""><a href="https://swift.org/migration-guide/se-0107-migrate.html#common-use-cases" class="">https://swift.org/migration-guide/se-0107-migrate.html#common-use-cases</a></div><div class=""><br class=""></div><div class="">The best ways to convert Swift to C strings are:</div><div class=""><br class=""></div><div class="">1. Pass the Swift string as a function argument of type</div><div class=""> Unsafe[Mutable]Pointer<Int8>.</div><div class=""><br class=""></div><div class="">2. Use `String.withCString` to create a block of Swift code that can</div><div class=""> access the C string.</div><div class=""><br class=""></div><div class="">That doesn't help you if you want to store a pointer to the CString in a property without defining its scope. In that case, you just need to explicitly copy the string.</div><div class=""><br class=""></div><div class="">I like Jordan's recommendation for calling strdup. That's the canonical way of creating a new C string with its own lifetime.</div><div class=""><br class=""></div><div class="">Guillaume's explanation with withMemoryRebound(to:) is also correct, if you want to work at that level.</div><div class=""><br class=""></div><div class="">We probably should provide an API that handles the special case of String literals so you don't need to copy. As Jordan suggested, that we could simply add a `cstring` property to StaticString. Feel free to file a bug for that so it isn't forgotten.</div></div><div class=""><br class=""></div><div class="">-Andy</div></body></html>