IOS Toll-Free Bridge

Source: Internet
Author: User

https://developer.apple.com/library/ios/documentation/CoreFoundation/Conceptual/CFDesignConcepts/Articles/ Tollfreebridgedtypes.html

Toll-Free bridge is essentially the conversion of the types in the core Foundation framework and the Cocoa Foundation framework, and in some functions that contain toll-free type parameters, you can use the core The type of foundation can also be used with the type of Cocoa Foundation

Ps:

Core Foundation framework: A set of C language interfaces, such as some classes at the beginning of CF, such as Cfstring

COCOA Foundation framework: OBJECITVE-C interfaces, such as those at the beginning of the NS, such as NSString

Example ( non-arc):

NSString *string = @ "Test string";

Cfstringref cfstring = (cfstringref) string;

The above code in the case of non-arc application, if in the case of arc need to use some keywords (_bridge, _bridge_retained, _bridge_transfer) conversion, or the compiler will error

_bridge: Only type conversions, object ownership unchanged

_bridge_retained: Cocoa Foundation object is converted to CF object, the memory of the CF object needs to be released by itself (Cfrelease)

_bridge_transfer: CF objects are converted to Cocoa Foundation objects, COCOA Foundation's object memory is managed by arc

Note: CF objects are not managed by arc

Example (ARC):

_bridge:

NSString *string = @ "Test string";

Cfstringref cfstring = (_bridge cfstringref) string;

...

Cfrelease (cfstring);

If the string object is freed, then Cfstring will be released.

_bridge_retained:

NSString *string = @ "Test string";

Cfstringref cfstring = (_bridge_retained cfstringref) string;

...

Cfrelease (cfstring);

If the string object is freed, cfstring will not be freed because _bridge_retained will add a reference to the

_bridge_transfer:

Cfstringref cfstring = cfstringcreate ... () ...;//or Cfstringref cfstring = Cfstr ("Hello, World."); Cfstr created string cannot be modified, not released through cfstring until end of program is released

NSString *string = (_bridge_transfer nsstring*) cfstring;

At this point the string is managed by the arc, and even if Cfstring is freed, the string will still exist

IOS Toll-Free Bridge

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.