CFString provides a suite of efficient string-manipulation and string-conversion functions. It offers seamless Unicode support and facilitates the sharing of data between Cocoa and C-based programs. CFString objects are immutable—use
CFMutableString to create and manage a string that can be changed after it has been created.
CFString has two primitive functions,
CFStringGetCharacterAtIndex(_:_:), that provide the basis for all other functions in its interface. The
CFStringGetLength function returns the total number (in terms of UTF-16 code pairs) of characters in the string. The
CFStringGetCharacterAtIndex function gives access to each character in the string by index, with index values starting at
CFString provides functions for finding and comparing strings. It also provides functions for reading numeric values from strings, for combining strings in various ways, and for converting a string to different forms (such as encoding and case changes). A number of functions, for example
CFStringFindWithOptions, allow you to specify a range over which to operate within a string. The specified range must not exceed the length of the string. Debugging options may help you to catch any errors that arise if a range does exceed a string’s length.
Like other Core Foundation types, you can hash CFStrings using the
CFHash(_:) function. You should never, though, store a hash value outside of your application and expect it to be useful if you read it back in later (hash values may change between different releases of the operating system).
CFString is “toll-free bridged” with its Cocoa Foundation counterpart,
NSString. This means that the Core Foundation type is interchangeable in function or method calls with the bridged Foundation object. Therefore, in a method where you see an
NSString * parameter, you can pass in a
CFStringRef, and in a function where you see a
CFStringRef parameter, you can pass in an NSString instance. This also applies to concrete subclasses of NSString. See Toll-Free Bridged Types for more information on toll-free bridging.