Contents

CFStringCreateCopy(_:_:)

Creates an immutable copy of a string.

Declaration

func CFStringCreateCopy(_ alloc: CFAllocator!, _ theString: CFString!) -> CFString!

Parameters

  • alloc:

    The allocator to use to allocate memory for the new string. Pass NULL or Kcfallocatordefault to use the current default allocator.

  • theString:

    The string to copy.

Return Value

An immutable string whose contents are identical to theString. Returns NULL if there was a problem copying the object. Ownership follows the The Create Rule.

Discussion

The resulting object has the same Unicode contents as the original object, but it is always immutable. It might also have different storage characteristics, and hence might reply differently to functions such as CFStringGetCStringPtr(_:_:). Also, if the specified allocator and the allocator of the original object are the same, and the string is already immutable, this function may simply increment the retention count without making a true copy. However, the resulting object is a true immutable copy, except the operation was a lot more efficient.

You should use this function in situations where a string is or could be mutable, and you need to take a snapshot of its current value. For example, you might decide to pass a copy of a string to a function that stores its current value in a list for later use.

See Also

Creating a CFString