Core foundation framework

Source: Internet
Author: User

From http://blog.csdn.net/diyagoanyhacker/article/details/7481637

Core foundation framework(Corefoundation. FrameworkIs a set of C language interfaces, which are IOS applications.ProgramProvides basic data management and service functions. The following lists the data and services that the Framework supports for management:

    • Group data type (array, set, etc)

    • Package

    • String Management

    • Date and Time Management

    • Raw data block management

    • Preference Management

    • URL and data stream operations

    • Thread and runloop

    • Port and soket Communication

The core foundation framework is closely related to the foundation framework. They provide interfaces for the same functions, but the foundation framework provides objective-C interfaces. If you mix the foundation object with the core foundation type, you can use the "toll-free bridging" between the two frameworks ". The so-calledToll-free bridgingIt means that you can use some types in the core foundatio and foundation frameworks at the same time in the methods or functions of a framework. Many data types support this feature, including group and string data types. The class and type descriptions of each framework describe whether an object is toll-free bridged and what object to bridge.

For more information, seeCore foundation framework reference.

 

After xcode4.2 began to import the arc mechanism, Apple added many key words for transformation to support inter-object transformation. In this case, we will understand its usage and reasons.

Introduction

Let's take a look at the method for converting ID type to void * type when the arc is invalid:

    1. Id OBJ=[[NsobjectAlloc]Init];
    2. Void*P=OBJ;

In turn, when the void * object is changed back to the ID type, it is simply written as below,

    1. Id OBJ=P;
    2. [OBJ release];

HoweverCodeWhen the arc is valid, the following error occurs:

  1. Error:ImplicitConversion ofObjective-C pointer
  2. To'Void*'IsDisallowedWithArc
  3. Void*P=OBJ;
  4. ^
  5.  
  6. Error:ImplicitConversion of a non-Objective-C pointer
  7. Type'Void*'To'ID'IsDisallowedWithArc
  8. Id o=P;
  9. ^

 

_ Bridge

to solve this problem, we use the __ bridge keyword to convert the ID and void * types. Let's look at the example below.

    1. id obj = [[ nsobject alloc ] init ];
    2. void * P = ( __ bridge void *) OBJ ;
    3. ID o = ( __ bridge id ) P ;

Convert the objective-C object type to the void * type using _ bridge and modify the variable using the _ unsafe_unretained keyword. The owner of the object to be substituted must specify the object lifecycle management and avoid abnormal access.

Besides _ bridge, there are two type conversion keywords related to _ Bridge:

    • _ Bridge_retained
    • _ Bridge_transfer

Next, let's take a look at the differences between the two keywords.

_ Bridge_retained

Let's look at the example program using the keyword _ bridge_retained:

    1. Id OBJ=[[NsobjectAlloc]Init];
    2.  
    3. Void*P=(_ Bridge_retainedVoid*)OBJ;

We should be able to understand its meaning in terms of name: when a type is converted, the ownership of its object will also be held by the variable after the transformation. If it is not the arc Code, it is similar to the following implementation:

    1. Id OBJ=[[NsobjectAlloc]Init];
    2.  
    3. Void*P=OBJ;
    4. [(ID)P retain];

An actual example can be used to verify whether the object ownership is held.

  1. Void*P=0;
  2.  
  3. {
  4. Id OBJ=[[NsobjectAlloc]Init];
  5. P=(_ Bridge_retainedVoid*)OBJ;
  6. }
  7.  
  8. Nslog(@"Class = % @",[(_ Bridge ID)PClass]);

After the braces are defined, P still points to a valid entity. It indicates that he owns the object. The object is not destroyed because of its defined range.

_ Bridge_transfer

Instead, you need to use the _ bridge_transfer keyword to release the variable that originally owns the object after type conversion. The text is a bit confusing. Let's take a look at a piece of code.

If the arc is invalid, we may need to write the following code.

    1. // The P variable originally holds the ownership of the object
    2. Id OBJ=(ID)P;
    3. [OBJ retain];
    4. [(ID)P release];

After the arc is valid, we can replace it with the following code:

    1. // The P variable originally holds the ownership of the object
    2. Id OBJ=(_ Bridge_transfer ID)P;

It can be seen that, __bridge_retained is the retain operation performed by the compiler for us, while _ bridge_transfer is release1 for us.

Toll-free bridged

In the IOS world, there are two main types of objects: Objective-C object and core Foundation object 0. Core Foundation objects are mainly objects of core foundation framework implemented in C language. They also have the concept of object reference count, but they are not cocoa framework: retain/release of foundation framework, instead, it is its own cfretain/cfrelease interface.

These two types of objects can be converted and operated on each other. When we do not use arc, we simply use C-type conversion and do not need to consume CPU resources. Therefore, they are called toll-free bridged. For example, nsarray, cfarrayref, nsstring, and cfstringref belong to different frameworks but have the same object structure. Therefore, standard C type conversion can be used.

For example, if we do not use arc, we use the following code:

    1. Nsstring*String=[NsstringStringwithformat:...];
    2. CfstringrefCfstring=(Cfstringref)String;

Similarly, when the core foundation type is converted to the objective-C type, it is simply possible to use the standard C type conversion.

However, when the arc is valid, a compilation error similar to the following occurs:

  1. CastOfObjective-C pointer type'Nsstring*'To C pointer type'Cfstringref'(AKA'ConstStruct_ Cfstring*')Requires a bridged cast
  2. Use_ Bridge to convert directly(NoChangeInOwnership)
  3. Use_ Bridge_retained to make an arcObjectAvailableAsA+1'Cfstringref'(AKA'ConstStruct_ Cfstring*')

The error shows how to perform the transformation with _ bridge or _ bridge_retained. The difference is that the ownership of the change object.

Because objective-C is the object managed by the arc, and core foundation is not the object managed by the arc, We need to specifically convert it like this. This is a concept of converting the ID type to void. That is to say, when the two types (with ARC Management, without ARC Management) are being converted, the compiler needs to tell how to handle the ownership of the object.

In the preceding example, the code after _ bridge/_ bridge_retained is as follows:

    1. Nsstring*String=[NsstringStringwithformat:...];
    2. CfstringrefCfstring=(_ BridgeCfstringref)String;

Only type conversion is executed, and ownership is not transferred. That is to say, cfstring cannot be used when the string object is released.

    1. nsstring * string = [ nsstring stringwithformat :...];
    2. cfstringref cfstring = ( __ bridge_retained cfstringref ) string ;
    3. ...
    4. cfrelease ( cfstring ); // because the core foundation object does not belong to the management scope of arc, therefore, you need to release your own

Use _ bridge_retained to transfer ownership by converting the retain at the target (cfstring. Even if the string variable is released, cfstring can still use a specific object. The only difference is that the core foundation object does not belong to the management scope of arc, so you need to release it yourself.

In fact, the core foundation provides the following functions to convert the core foundation object type and the objective-C object type.

  1. CftyperefCfbridgingretain(Id x){
  2. Return(_ Bridge_retainedCftyperef)X;
  3. }
  4.  
  5. IDCfbridgingrelease(CftyperefX){
  6. Return(_ Bridge_transfer ID)X;
  7. }

Therefore, you can use cfbridgingretain to replace the _ bridge_retained Keyword:

    1. Nsstring*String=[NsstringStringwithformat:...];
    2. CfstringrefCfstring=Cfbridgingretain(String);
    3. ...
    4. Cfrelease(Cfstring);// Because the core foundation is not managed by the arc, you need to take the initiative to release it.
_ Bridge_transfer

When ownership is transferred, the converted variable will lose the ownership of the object. When the core foundation object type is converted to the objective-C object type, the _ bridge_transfer keyword is often used.

    1. CfstringrefCfstring=Cfstringcreate...();
    2. Nsstring*String=(_ Bridge_transferNsstring*)Cfstring;
    3.  
    4. // Cfrelease (cfstring); because the ownership of the object has been transferred with _ bridge_transfer, you do not need to call release

Similarly, we can use cfbridgingrelease () to replace the _ bridge_transfer keyword.

    1. CfstringrefCfstring=Cfstringcreate...();
    2. Nsstring*String=Cfbridgingrelease(Cfstring);
Summary

From the above learning, we learned how to use type conversion in Arc. What principles or methods should we use in actual use? Below I have summarized several key elements.

    • Determine whether the conversion type is an object managed by arc.

      • Core Foundation object types are not managed by arc
      • Cocoa framework: The Foundation object type (that is, the commonly used objectie-C object type) is under the management scope of arc.
    • If the objects are not in the scope of ARC Management, it is necessary to know who is responsible for release.
    • What is the lifecycle of various objects?

1. when ID obj is declared, A _ strong modified variable is declared by default, so the compiler automatically adds retain's processing, so the _ bridge_transfer keyword only performs release processing for us.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.