Contents

CFStringGetCString(_:_:_:_:)

Copies the character contents of a string to a local C string buffer after converting the characters to a given encoding.

Declaration

func CFStringGetCString(_ theString: CFString!, _ buffer: UnsafeMutablePointer<CChar>!, _ bufferSize: CFIndex, _ encoding: CFStringEncoding) -> Bool

Parameters

  • theString:

    The string whose contents you wish to access.

  • buffer:

    The C string buffer into which to copy the string. On return, the buffer contains the converted characters. If there is an error in conversion, the buffer contains only partial results.

    The buffer must be large enough to contain the converted characters and a NUL terminator. For example, if the string is Toby, the buffer must be at least 5 bytes long.

  • bufferSize:

    The length of buffer in bytes.

  • encoding:

    The string encoding to which the character contents of theString should be converted. The encoding must specify an 8-bit encoding.

Return Value

true upon success or false if the conversion fails or the provided buffer is too small.

Discussion

This function is useful when you need your own copy of a string’s character data as a C string. You also typically call it as a “backup” when a prior call to the CFStringGetCStringPtr(_:_:) function fails.

See Also

Accessing Characters