Consider the following code snippet:
let url = FileManager.default.homeDirectoryForCurrentUser
let cString1 = url.absoluteString.cString(using: .utf8)
let cString2 = url.withUnsafeFileSystemRepresentation { $0 }
Can we expect cString1 and cString2 to be equal C strings?
As described in the docs of withUnsafeFileSystemRepresentation(_:) it is converting the Swift string to a C string with UTF8 encoding. That's exactly the same as what cString(using: .utf8) is doing.
Both convert the Swift string to the type UnsafePointer<Int8>?.
Only Xcode is displaying the return type of cString(using:) as [CChar]? where CChar is a type alias for Int8. Also a Swift array can just passed to an UnsafePointer as far as I know.
Mostly I just use cString(using: .utf8) and everything is fine but there are rare cases where I need to use withUnsafeFileSystemRepresentation(_:) for some reason, otherwise the C function is not understanding the string.
So what is the difference I'm missing here?