This is working because of one of the interoperability changes the Swift team has made since the initial launch – you’re right that it looks like it hasn’t made it into the documentation yet. String
works where an UnsafePointer<UInt8>
is required so that you can call C functions that expect a const char *
parameter without a lot of extra work.
Look at the C function strlen
, defined in “shims.h”:
size_t strlen(const char *s);
In Swift it comes through as this:
func strlen(s: UnsafePointer<Int8>) -> UInt
Which can be called with a String
with no additional work:
let str = "Hi."
strlen(str)
// 3
Look at the revisions on this answer to see how C-string interop has changed over time: https://stackoverflow.com/a/24438698/59541