Copies the character contents of a string to a local C string buffer after converting the characters to a given encoding.
SDKs
- iOS 2.0+
- macOS 10.0+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
Framework
- Core Foundation
Declaration
Boolean CFStringGetCString(CFString Ref theString, char *buffer, CFIndex bufferSize, CFString Encoding encoding);
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 isToby
, 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
the
should be converted. The encoding must specify an 8-bit encoding.String
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 CFString
function fails.