Free bridging in iOS and free bridging in iOS
Excerpted from iOS 6 programming practices.
Unlike the Objective-C library, the standard C language and Core Foundation Class Library (CF * method) in Objective-C do not follow those naming conventions. This means that the ARC compiler cannot release unwanted memory. Before the emergence of ARC, We can forcibly convert the CF * object to the NS * object, which is called toll-free bridging ). That is to say, we can convert the CFString * type to the NSString * type through type conversion. With ARC, we can no longer do this, at least not specify a ownership transfer modifier.
ARC allows the following ownership transfer modifiers: __bridge, _ bridge_retained, _ bridge_transfer
1. _ bridge
The first modifier _ bridge is a normal conversion, which tells ARC not to increase its reference count value or change its ownership. LLVM designers didn't make _ bridge the default ownership transfer modifier, because the decision was too bold. The Core Foundation method does not have an appropriate naming convention. Therefore, without explicitly specifying a ownership transfer modifier, the compiler cannot make a decision on how to transfer ownership of a C pointer type.
2. _ bridge_retained
To convert the C pointer type and increase the reference count value, we can use the second modifier. This modifier can be used to return a reserved pointer from the Objective-C method (which creates a Core Foundation object and releases it later using the CFRelease method. If the Objective-C method belongs to the NSRETURNS_RETAINED series, a reserved pointer is returned.
3. _ bridge_transfer
If you want to convert the Core Foundation pointer type to an Objective-C pointer and Add 1 to the reference count, you can use the last modifier. If the Core Foundation method creates an object and wants to use ARC to manage the object's memory, we can use this modifier. Essentially, we "transfer" ownership to ARC.