美文网首页
Core Foundation和Foundation中可互换使用

Core Foundation和Foundation中可互换使用

作者: 某个胖子 | 来源:发表于2015-10-14 15:57 被阅读111次

    在Core Foundation和Foundation中有许多可互换使用的数据类型。这意味着可以使用相同的数据类型,作为Core Foundation框架方法调用或Objective-C消息的参数或接收者。这种在Core Foundation框架和Foundation框架之间交换使用数据类型的技术就叫 Toll-Free Bridging(无缝桥接)。
    #if __has_feature(objc_arc)
    // After using a CFBridgingRetain on an NSObject, the caller must take responsibility for calling CFRelease at an appropriate time.
    NS_INLINE CF_RETURNS_RETAINED CFTypeRef CFBridgingRetain(id X) {
    return (__bridge_retained CFTypeRef)X;
    }

     NS_INLINE id CFBridgingRelease(CFTypeRef CF_CONSUMED X) {
    return (__bridge_transfer id)X;
    }
    
    #else
    
    // This function is intended for use while converting to ARC mode only.
    NS_INLINE CF_RETURNS_RETAINED CFTypeRef   
    CFBridgingRetain(id X) {
    return X ? CFRetain((CFTypeRef)X) : NULL;
    }
    
    // This function is intended for use while converting to ARC mode only.
    NS_INLINE id CFBridgingRelease(CFTypeRef CF_CONSUMED X) {
    return [(id)CFMakeCollectable(X) autorelease];
    }
    
    #endif
    

    但并不是所有的CF对象都支持 Toll-Free Bridging!官方介绍:[https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/Toll-FreeBridgin/Toll-FreeBridgin.html]

    相关文章

      网友评论

          本文标题:Core Foundation和Foundation中可互换使用

          本文链接:https://www.haomeiwen.com/subject/qfjmcttx.html