Tag Archives: Toll-Free Bridging

Bridging mechanism of toll free bridging in IOS

Toll free bridging, referred to as TFB, is a mechanism that allows some objc classes and their corresponding corefoundation classes to be used interchangeably. For example, nsstring and cfstring are bridged, which means that any nsstring can be used as a cfstring, or any cfstring can be used as an nsstring

There are a number of data types in the core foundation framework and the foundation framework that can be used interchangeably. This capability, called toll free bridging, means that you can use the same data type as the parameter to a core foundation function call or as the receiver of an Objective-C message

The principle (take nsstring as an example) is: nsstring is an abstract class. Whenever you create an instance of nsstring, it is actually a private subclass instance of nsstring. One of the private subclasses is nscfstring, which is the corresponding class of cfstring class in objc. Nscfstring implements all the methods needed as an nscfstring

My understanding: in short, you know that there is a toll free bridging mechanism, and nscfstring is a private subclass of nsstring, which implements all its methods. See the official website for detailed explanation

Why cfstring

Explanation on the official website:

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

On this point, I also recommend a better blog post:

Toll Free Bridging