cocoa-touch - core foundation与objective-c区别?
怪我咯
怪我咯 2017-04-21 11:16:12
0
1
586

core foundation与objective-c之间有什么关系和区别?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(1)
PHPzhong

Core Foundation framework (CoreFoundation.framework) is a set of C language interfaces that provide basic data management and service functions for iOS applications. The following is a list of the data that the framework supports for management and the services it can provide:

Group data types (arrays, sets, etc.)

Packages

String Management

Date and Time Management

Raw data block management

Preference Management

URL and data flow operations

Threads and RunLoop

Port and socket communication

Core Foundation framework and Foundation framework are closely related, they provide interfaces for the same functions, but Foundation framework provides Objective-C interface. If you mix Foundation objects with Core Foundation types, you can take advantage of "toll-free bridging" between the two frameworks. The so-called Toll-free bridging means that you can use certain types in the Core Foundation and Foundation frameworks at the same time in a method or function of a certain framework. Many data types support this feature, including group and string data types. Each framework's class and type description will describe whether an object is toll-free bridged and what objects it should be bridged to.

Conversion between Objective-C pointers and CoreFoundation pointers

ARC only manages Objective-C pointers (retain, release, autorelease) and does not manage CoreFoundation pointers. CF pointers are manually managed and managed manually by CFRetain and CFRelease. Note that there is no autorelease in CF.

When converting CocoaFoundation pointers and CoreFoundation pointers, you need to consider the ownership of the pointed objects. ARC provides 3 modifiers for management.

1. __bridge, does nothing, just converts. In this case:

i). To convert from Cocoa to Core, manual CFRetain is required. Otherwise, after the Cocoa pointer is released, the passed pointer will be invalid.

ii). Converting from Core to Cocoa requires manual CFRelease. Otherwise, after the Cocoa pointer is released, the object reference count is still 1 and will not be destroyed.

2. __bridge_retained, automatically calls CFRetain after conversion, which helps automatically solve the above i situation.

3. __bridge_transfer automatically calls CFRelease after conversion, which helps automatically solve the above ii situation.

Since ARC cannot manage the life cycle of Core Foundation Object, we need to use the three conversion keywords __bridge, __bridge_retained and __bridge_transfer between Core Foundation and ARC.

According to Apple’s official documentation (https://developer.apple.com/library/i...):

__bridge only performs type conversion, but does not modify object (memory) management rights;

__bridge_retained (CFBridgingRetain can also be used) converts Objective-C objects into Core Foundation objects, and at the same time hands over the management of the objects (memory) to us. Subsequently, we need to use CFRelease or related methods to release the objects;

__bridge_transfer (CFBridgingRelease can also be used) converts Core Foundation objects to Objective-C objects, while handing over the management of the objects (memory) to ARC.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template