This article goes to: http://www.qingpingshan.com/rjbc/ios/157477.html
Introduction
The Core Foundation Framework (corefoundation.framework) is a set of C-language interfaces that provide basic data management and service capabilities for iOS applications. The following lists the data that the framework supports for management and what services are available:
1. Group data types (arrays, collections, etc.)
2. Package
3. String Management
4. Date and Time management
5. Raw Data Block Management
6. Preference Management
7.URL and Data flow operations
8. Threading and Runloop
9. Port and Soket communication
The Core Foundation framework is closely related to the foundation framework, which provides interfaces for the same functionality, but the foundation framework provides the OBJECTIVE-C interface. If you use the foundation object with the core foundation type, you can take advantage of the "toll-free bridging" between the two frameworks. The so-called toll-free bridging is that you can use some of the types in the core Foundatio and foundation frameworks at the same time in a framework's methods or functions. Many data types support this feature, including group and string data types. The class and type descriptions for each frame are toll-free bridged for an object and what object bridging should be described.
Toll-Free bridging is the bridge between the arc under OC Object and the core foundation object
We sometimes use core foundation objects when developing iOS applications, hereinafter referred to as CF. such as Core Graphics, Core Text, and we may need to convert CF objects and OC objects, we know that in the ARC environment, the compiler does not automatically manage the memory of CF objects, we need to manage them manually. This is where we need to manually release the CF object after we create it using Cfrelease.
So how do you manage memory when CF and OC Convert to each other?
We can do memory management through Bridge, bridge_transfer,__bridge_retained.
1.__bridge
CF and OC object conversions only involve conversions where object types do not involve object ownership
Image I/O reads picture data from NSBundle NSString *ImagePath= [[NSBundleMainbundle]Pathforresource:@"Image"OfType:@"PNG"]; CgimagesourcerefSource= cgimagesourcecreatewithurl ( Span class= "PLN" >__bridge cfurlref) [nsurl Fileurlwithpath:[[ nsbundle Mainbundle] pathforresource< Span class= "pun" >:@ "image" :@ "PNG" ]], null
If the above does not add bridge, in the ARC environment, the system will give error and error correction, click on the error prompt, the system will automatically add bridge for us, because the conversion of OC and CF only involves the object type does not involve the conversion of object ownership, Therefore, the above code does not need to release the CF object, that is, no need to add cfrelease
Comments:IOS ARCAnd Non -ARC 1target,2build phasescomplie sourcesarc -arc if not arc Then enter:-< span class= "PLN" >fno-objc-arc
To solve this problem, we use the __bridge keyword to convert the ID type to the void* type.
IdObj= [[NSObjectalloc] init]; void *p = (__bridge void *) (obj); NSLog(@"obj retaincount%ld", [(ID)p retaincount]);
Output Result:
Cfdemo1
2.__bridge_transfer
Often when the CF object is converted to OC objects, the ownership of the CF object is given to the OC object, at which point the arc can automatically manage the memory, acting as cfbridgingrelease ()
If it is not arc, we may need to write the following code.
The P variable originally held the object's ownership id=(ID)p; [retain]; [(ID)p release];
Then, after arc is valid, we can replace it with the following code:
P variable Ownership ID of the object originally held ) p;
As can be seen, bridge_retained is the compiler for us to do the retain operation, and Bridge_transfer is for us to do the release.
3.
bridge_retained
And
Bridge_transfer instead, the OC object is often converted to a CF object, and the ownership of the OC object is also given to the CF object to manage, that is, when the OC object is converted into a CF object, it involves the conversion of object type and object ownership, acting as Cfbridgingretain ()
First look at the example program using the __bridge_retained keyword:
ID=[[nsobject alloc] init]; void*=(void*)obj;
At this time Retaincount will be added 1;
From the name we should be able to understand its meaning: when a type is converted, the ownership of its object is also held by the transformed variable. If it is not the arc code, it resembles the following implementation:
ID obj = [[nsobject alloc] init]; void *p = obj; [(retain];
How Arc obtains Retaincount
NSLog(@"Retain count is%ld",cfgetretaincount(__bridgecftyperef) MyObject));
Let me give you an example:
NSString *String = [NSStringstringWithFormat:@""]; CfstringrefCfstring= (__bridgecfstringref) string; Span class= "hljs-built_in" >cfstringref Cfstr = (__bridge_retained cfstringref) string; Span class= "hljs-built_in" >cfrelease (cfstring //because core foundation objects are not part of the ARC Management category, you need to Releasecfrelease (cfstr
Use __bridge_retained to transfer ownership by converting the retain processing at the target (CFSTR). Even if the string variable is freed, the cfstring variable is released, and CFSTR can still use the specific object. Just one thing, because core Foundation objects are not part of the ARC Management category, you need to release them yourself.
CfstringrefCfstring= Cfurlcreatestringbyaddingpercentescapes( Null, (__bridgeCfstringref)Text, null, CFSTR ( "!*" ();: @&=+$,/?%#[] "), Span class= "PLN" > cfstringconvertnsstringencodingtoencodingnsutf8stringencodingnsstring *= (__bridge_transfer cfstringref) cfstring
While ownership is being shifted, the converted variable loses ownership of the object. The __bridge_transfer keyword is often used when the core foundation object type is converted to the Objective-c object type.
Summarize:
- Core Foundation object types are not in the ARC management category
- Cocoa Framework::foundation Object Type (that is, commonly used objectie-c object type) within the scope of ARC management
3. Bridge, Bridge_transfer and __bridge_retained are the bridges of CF and OC
- If you are not in the ARC management category, be aware of who is responsible for the release and what the life cycle of the various objects is.
This is currently in the learning ImageIO Apple's official picture decoder, encountered between OC and CF conversion of some problems, re-collation, meticulous understanding, in order to facilitate further in-depth learning.
iOS Development __bridge,__bridge_transfer and __bridge_retained