Search code examples
core-foundationfoundationbridge

Core Foundation to Foundation and vice-versa


I just learned about the __bridge cast today and I am not understanding the implications behind it:

Why would Apple create Two frameworks that are so close to one another that they are toll-free interchangeable.

What benefit does one gain by using one framework over the other?

Are there any good tutorials out there that show strategies to use when bridging between the two frameworks?


Solution

  • Some years ago, you had the choice to program code in Apple environment either in C or in Objective-C. From basic low-level to graphic application on the Mac, you'd had access to both API.

    Both API needed the same abstractions and concepts to not deny developers that would use C or Objective-C. As Objective-C is a superset of C, it was easy to embed one C structure in a Objective-C class. This avoid to recreate a different abstraction and make the interaction between both easier.

    The benefits of C version, is that you are working at a lower layer that make your code technically lighter and faster as you don't need to use the Objective-C runtime. I said technically because it is lower level than Objective-C, but I'm not sure you can grab the difference today in current application. The benefit of Objective-C version is dynamicity and object oriented code. You could do OO with the C version, but it would be more verbose.

    The best documentation for bridging is the LLVM arc documentation: http://clang.llvm.org/docs/AutomaticReferenceCounting.html

    Mike ash has a very good explanation on toll free bridging here: https://mikeash.com/pyblog/friday-qa-2010-01-22-toll-free-bridging-internals.html