CFStringCreateExternalRepresentation(_:_:_:_:)
Creates an “external representation” of a CFString object, that is, a CFData object.
Declaration
func CFStringCreateExternalRepresentation(_ alloc: CFAllocator!, _ theString: CFString!, _ encoding: CFStringEncoding, _ lossByte: UInt8) -> CFData!Parameters
- alloc:
The allocator to use to allocate memory for the new CFData object. Pass
NULLor Kcfallocatordefault to use the current default allocator. - theString:
The string to convert to an external representation.
- encoding:
The string encoding to use for the external representation.
- lossByte:
The character value to assign to characters that cannot be converted to the requested encoding. Pass
0if you want conversion to stop at the first such error; if this happens, the function returnsNULL.
Return Value
A CFData object that stores the characters of the CFString object as an “external representation.” Returns NULL if no loss byte was specified and the function could not convert the characters to the specified encoding. Ownership follows the The Create Rule.
Discussion
In the CFData object form, the string can be written to disk as a file or be sent out over a network. If the encoding of the characters in the data object is Unicode, the function may insert a BOM (byte-order marker) to indicate endianness. However, representations created with encoding constants kCFStringEncodingUTF16BE, kCFStringEncodingUTF16LE, kCFStringEncodingUTF32BE, and kCFStringEncodingUTF32LE do not include a BOM because the byte order is explicitly indicated by the letters “BE” (big-endian) and “LE” (little-endian).
This function allows the specification of a “loss byte” to represent characters that cannot be converted to the requested encoding.
When you create an external representation from a CFMutableString object, it loses this mutability characteristic when it is converted back to a CFString object.
The CFStringCreateFromExternalRepresentation(_:_:_:) function complements this function by creating a CFString object from an “external representation” CFData object.
See Also
Accessing Characters
CFStringGetBytes(_:_:_:_:_:_:_:_:)CFStringGetCharacterAtIndex(_:_:)CFStringGetCharacters(_:_:_:)CFStringGetCharactersPtr(_:)CFStringGetCharacterFromInlineBuffer(_:_:)CFStringGetCString(_:_:_:_:)CFStringGetCStringPtr(_:_:)CFStringGetLength(_:)CFStringGetPascalString(_:_:_:_:)CFStringGetPascalStringPtr(_:_:)CFStringGetRangeOfComposedCharactersAtIndex(_:_:)CFStringInitInlineBuffer(_:_:_:)